ContentLine.spec.ts 6.01 KB
Newer Older
Mark Stenglein's avatar
Mark Stenglein committed
1 2
/*
 * lib-ical
Mark Stenglein's avatar
Mark Stenglein committed
3
 * Copyright (C) 2017 Mark Stenglein
Mark Stenglein's avatar
Mark Stenglein committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
18
import ContentLine from "../src/ContentLine";
19
import Parameter from "../src/Parameter";
20 21 22
import { expect } from "chai";
import "mocha";

Mark Stenglein's avatar
Mark Stenglein committed
23 24 25
/**
 * Test The ContentLine class
 */
26 27
describe("ContentLine", () => {
    it("Should exist", () => {
Mark Stenglein's avatar
Mark Stenglein committed
28 29
        expect(ContentLine).to.exist;
    });
30

Mark Stenglein's avatar
Mark Stenglein committed
31 32 33 34 35
    /**
     * Test ContentLine's constructor
     */
    describe("constructor", () => {
        it("Should create an object", () => {
36
            const param: Parameter = new Parameter("testParam", ["test1"]);
37
            const test: ContentLine = new ContentLine("name", [param], "value");
Mark Stenglein's avatar
Mark Stenglein committed
38
            let result: boolean = true;
39

Mark Stenglein's avatar
Mark Stenglein committed
40 41 42 43 44 45 46 47
            if (typeof test === undefined) {
                result = false;
            }

            expect(result).to.be.true;
        });

        it("Should have the same name as given on input", () => {
48
            const param: Parameter = new Parameter("testParam", ["test1"]);
Mark Stenglein's avatar
Mark Stenglein committed
49
            const test: string = "name";
50
            const testLine: ContentLine = new ContentLine(test, [param], "value");
Mark Stenglein's avatar
Mark Stenglein committed
51 52 53 54 55 56
            const result: string = testLine.name;

            expect(result).to.be.equal(test);
        });

        it("Should have the same param as given on input", () => {
57
            const testParam1: Parameter[] = [new Parameter("testParam", ["test1"])];
Mark Stenglein's avatar
Mark Stenglein committed
58
            const testLine: ContentLine = new ContentLine("name", testParam1, "value");
59
            const result: Parameter[] = testLine.params;
Mark Stenglein's avatar
Mark Stenglein committed
60

Mark Stenglein's avatar
Mark Stenglein committed
61 62
            expect(result).to.be.deep.equal(testParam1);
        });
Mark Stenglein's avatar
Mark Stenglein committed
63

Mark Stenglein's avatar
Mark Stenglein committed
64
        it("Should accept multiple param values on input", () => {
65 66 67 68
            const param1: Parameter = new Parameter("param1", ["value1"]);
            const param2: Parameter = new Parameter("param2", ["value2"]);
            const testParams: Parameter[] = [param1, param2];

Mark Stenglein's avatar
Mark Stenglein committed
69
            const testLine: ContentLine = new ContentLine("name", testParams, "value");
70
            const result: Parameter[] = testLine.params;
Mark Stenglein's avatar
Mark Stenglein committed
71

Mark Stenglein's avatar
Mark Stenglein committed
72 73 74 75
            expect(result).to.be.deep.equal(testParams);
        });

        it("Should accept no param values as input", () => {
76
            const testParams: Parameter[] = [];
Mark Stenglein's avatar
Mark Stenglein committed
77
            const testLine: ContentLine = new ContentLine("name", testParams, "value");
78
            const result: Parameter[] = testLine.params;
Mark Stenglein's avatar
Mark Stenglein committed
79 80 81 82 83 84

            expect(result).to.be.deep.equal(testParams);
        });

        it("Should have the same value as given on input", () => {
            const testValue: string = "testValue";
85
            const testParam: Parameter = new Parameter("NAME", ["TEST"]);
86
            const testLine: ContentLine = new ContentLine("name", [testParam], testValue);
87
            const result: string = testLine.value;
Mark Stenglein's avatar
Mark Stenglein committed
88 89 90

            expect(result).to.be.equal(testValue);
        });
91
    }); /* describe("constructor") */
92

93 94 95 96 97 98 99 100 101
   /**
    * Test `fold` static method
    *
    * @author Mark Stenglein <mark@stenle.in>
    */
    describe("fold", () => {
        it("Should exist as a static method", () => {
            expect(ContentLine).itself.respondsTo("fold");
        });
Mark Stenglein's avatar
Mark Stenglein committed
102

103 104 105
        it("Should not fold short lines", () => {
            const test: string = "A short line";
            const result: string = ContentLine.fold(test);
Mark Stenglein's avatar
Mark Stenglein committed
106

107 108
            expect(test).to.be.equal(result);
        });
Mark Stenglein's avatar
Mark Stenglein committed
109

110 111
        it("Should fold lines longer than 74 characters", () => {
            const test: string = "This is a quite long string which should" +
Mark Stenglein's avatar
Mark Stenglein committed
112 113
                " be folded on the 74th character exactly.";

114
            const expected: string = "This is a quite long string which" +
Mark Stenglein's avatar
Mark Stenglein committed
115 116
                " should be folded on the 74th character e\r\n xactly.";

117
            const result: string = ContentLine.fold(test);
Mark Stenglein's avatar
Mark Stenglein committed
118

119 120
            expect(result).to.be.equal(expected);
        });
Mark Stenglein's avatar
Mark Stenglein committed
121

122 123
        it("Should fold really long lines at each fold spot.", () => {
            const test: string = "This string is longer than 10, being 74" +
Mark Stenglein's avatar
Mark Stenglein committed
124 125 126 127 128
                " characters long to be quite exact.This string is longer" +
                " than 10, being 74 characters long to be quite exact.This" +
                " string is longer than 10, being 74 characters long to be" +
                " quite exact.";

129
            const expected: string = "This string is longer than 10, being" +
Mark Stenglein's avatar
Mark Stenglein committed
130 131 132 133
                " 74 characters long to be quite exact.\r\n This string is" +
                " longer than 10, being 74 characters long to be quite" +
                " exact.\r\n This string is longer than 10, being 74" +
                " characters long to be quite exact.";
134

135
            const result: string = ContentLine.fold(test);
Mark Stenglein's avatar
Mark Stenglein committed
136

137 138 139
            expect(result).to.be.equal(expected);
        });
    }); /** describe("fold") */
140

141 142
    describe("generate", () => {
        it("Should respond as non-static method", () => {
143
             expect(ContentLine).respondsTo("generate");
144 145 146 147 148 149 150 151 152 153 154 155 156
        });

        it("Should generate properly formatted lines", () => {
            const param1: Parameter = new Parameter("PARAM-ONE", ["value1", "value2"]);
            const param2: Parameter = new Parameter("PARAM-TWO", ["value1"]);
            const params: Parameter[] = [param1, param2];
            const test: ContentLine = new ContentLine("NAME", params, "value");
            const expected = "NAME;PARAM-ONE=value1,value2;PARAM-TWO=value1:value";
            const result = test.generate();

            expect(result).to.be.equal(expected);
        });
    }); /** describe("generate") */
157
}); /** describe("ContentLine") */