ContentLine.spec.ts 5.36 KB
Newer Older
1
import ContentLine from "../src/ContentLine";
2
import Parameter from "../src/Parameter";
3 4 5
import { expect } from "chai";
import "mocha";

Mark Stenglein's avatar
Mark Stenglein committed
6 7 8
/**
 * Test The ContentLine class
 */
9 10
describe("ContentLine", () => {
    it("Should exist", () => {
Mark Stenglein's avatar
Mark Stenglein committed
11 12
        expect(ContentLine).to.exist;
    });
13

Mark Stenglein's avatar
Mark Stenglein committed
14 15 16 17 18
    /**
     * Test ContentLine's constructor
     */
    describe("constructor", () => {
        it("Should create an object", () => {
19 20
            let param: Parameter = new Parameter("testParam", ["test1"]);
            const test: ContentLine = new ContentLine("name", [param], "value");
Mark Stenglein's avatar
Mark Stenglein committed
21
            let result: boolean = true;
22

Mark Stenglein's avatar
Mark Stenglein committed
23 24 25 26 27 28 29 30
            if (typeof test === undefined) {
                result = false;
            }

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

        it("Should have the same name as given on input", () => {
31
            const param: Parameter = new Parameter("testParam", ["test1"]);
Mark Stenglein's avatar
Mark Stenglein committed
32
            const test: string = "name";
33
            const testLine: ContentLine = new ContentLine(test, [param], "value");
Mark Stenglein's avatar
Mark Stenglein committed
34 35 36 37 38 39
            const result: string = testLine.name;

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

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

Mark Stenglein's avatar
Mark Stenglein committed
44 45
            expect(result).to.be.deep.equal(testParam1);
        });
Mark Stenglein's avatar
Mark Stenglein committed
46

Mark Stenglein's avatar
Mark Stenglein committed
47
        it("Should accept multiple param values on input", () => {
48 49 50 51
            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
52
            const testLine: ContentLine = new ContentLine("name", testParams, "value");
53
            const result: Parameter[] = testLine.params;
Mark Stenglein's avatar
Mark Stenglein committed
54

Mark Stenglein's avatar
Mark Stenglein committed
55 56 57 58
            expect(result).to.be.deep.equal(testParams);
        });

        it("Should accept no param values as input", () => {
59
            const testParams: Parameter[] = [];
Mark Stenglein's avatar
Mark Stenglein committed
60
            const testLine: ContentLine = new ContentLine("name", testParams, "value");
61
            const result: Parameter[] = testLine.params;
Mark Stenglein's avatar
Mark Stenglein committed
62 63 64 65 66 67

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

        it("Should have the same value as given on input", () => {
            const testValue: string = "testValue";
68 69 70
            const testParam: Parameter = new Parameter("name", [testValue]);
            const testLine: ContentLine = new ContentLine("name", [testParam], testValue);
            const result: string = testLine.params[0].paramValues[0];
Mark Stenglein's avatar
Mark Stenglein committed
71 72 73

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

Mark Stenglein's avatar
Mark Stenglein committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    /**
     * Test `fold` static method
     *
     * @author Mark Stenglein <mark@stenle.in>
     */
     describe("fold", () => {
         it("Should exist as a static method", () => {
             expect(ContentLine).itself.respondsTo("fold");
         });

         it("Should not fold short lines", () => {
             const test: string = "A short line";
             const result: string = ContentLine.fold(test);

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

         it("Should fold lines longer than 74 characters", () => {
             const test: string = "This is a quite long string which should" +
                " be folded on the 74th character exactly.";

             const expected: string = "This is a quite long string which" +
                " should be folded on the 74th character e\r\n xactly.";

             const result: string = ContentLine.fold(test);

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

         it("Should fold really long lines at each fold spot.", () => {
             const test: string = "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.This" +
                " string is longer than 10, being 74 characters long to be" +
                " quite exact.";

             const expected: string = "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.\r\n This string is longer than 10, being 74" +
                " characters long to be quite exact.";
117

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

             expect(result).to.be.equal(expected);
121 122 123 124 125
         });
     }); /** describe("fold") */

     describe("generate", () => {
         it("Should respond as non-static method", () => {
126
             expect(ContentLine).respondsTo("generate");
127 128 129
         });

         it("Should generate properly formatted lines", () => {
130 131 132 133 134
             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";
135
             const result = test.generate();
Mark Stenglein's avatar
Mark Stenglein committed
136

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