Parameter.spec.ts 14.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
import Parameter from "../src/Parameter";
import { expect } from "chai";
import "mocha";

/**
 * Test the Parameter parent class.
 */
describe("Parameter", () => {
    it("Should exist", () => {
        expect(Parameter).to.exist;
    });

13 14 15 16 17 18
    /*
     * Note: it is not neccessary to test all of the behavior for the
     * constructor as far as validation of input data is concerned. This is
     * because the SET method for each property performs that function. Look
     * there for tests.
     */
19 20
    describe("constructor()", () => {
        it("Should create an object", () => {
21
            const result: Parameter = new Parameter("TestName", ["value"]);
22 23
            expect(result).to.exist;
        });
24 25 26 27 28

        it("Should have all the right properties", () => {
            const testName: string = "TEST-NAME";
            const testParam: Parameter = new Parameter(testName, ["value1"]);

29 30 31 32 33
            expect(testParam).to.have.property("_paramName");
            expect(testParam).to.have.property("_paramValues");
            expect(testParam).to.have.property("paramName");
            expect(testParam).to.have.property("paramValues");
            expect(testParam).to.have.property("generate");
34
        });
35 36
    });

37 38 39 40 41
    /*
     * All the GET methods are really doing is pulling the private data
     *  variables out for the requester to see. Nothing too complex is happening
     *  that needs to be tested.
     */
42 43
    describe("GET Methods", () => {
        describe("paramName", () => {
44 45 46 47 48 49 50 51
            it("Should return the correct name", () => {
                const testName: string = "TEST-NAME";
                const testValues: string[] = ["value1", "value2"];
                const testParam: Parameter = new Parameter(testName, testValues);
                const result: string = testParam.paramName;

                expect(result).to.equal(testName);
            });
52 53 54
        });

        describe("paramValues", () => {
55 56 57 58 59 60 61 62
            it("Should return the correct array of values", () => {
                const testName: string = "TEST-NAME";
                const testValues: string[] = ["value1", "value2"];
                const testParam: Parameter = new Parameter(testName, testValues);
                const result: string[] = testParam.paramValues;

                expect(result).to.deep.equal(testValues);
            });
63 64 65
        });
    });

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    /*
     * Note that the SET methods are difficult to directly test, as they are
     *  called even by the constructor function when setting the values. Because
     *  of that behavior, they can be tested directly by simply calling the
     *  class constructors with the values you want to test.
     *
     * The constructor is really only going so far as to do this:
     *
     *     constructor(inName: string, inValues: string[]) {
     *         this.paramName = inName;
     *         this.paramValues = inValues;
     *     }
     *
     * These tests implement the data validation that is missing from the
     *  constructor level tests.
     */
82 83
    describe("SET Methods", () => {
        describe("paramName", () => {
84
            it("Correctly sets iana-token names", () => {
Mark Stenglein's avatar
Mark Stenglein committed
85 86 87
                const name: string = "A0-TEST";
                const values: string[] = ["value"];
                const testParam: Parameter = new Parameter(name, values);
88

Mark Stenglein's avatar
Mark Stenglein committed
89
                expect(testParam.paramName).to.be.equal(name);
90 91 92
            });

            it("Correctly sets x-name names", () => {
Mark Stenglein's avatar
Mark Stenglein committed
93 94 95
                const name: string = "X-VENDOR-TEST";
                const values: string[] = ["value"];
                const testParam: Parameter = new Parameter(name, values);
96

Mark Stenglein's avatar
Mark Stenglein committed
97
                expect(testParam.paramName).to.be.equal(name);
98 99 100
            });

            it("Throws an exception on incorrect name type", () => {
Mark Stenglein's avatar
Mark Stenglein committed
101 102 103 104
                expect(() => {
                    const name: string = "!NVALID-N@ME";
                    const values: string[] = ["value"];
                    const testParam: Parameter = new Parameter(name, values);
105
                }).to.throw("Parameter must be valid iana-token or x-name");
106
            });
107 108
        });

109
        describe("***paramValues", () => {
110 111 112 113 114
            it("Correctly sets paramtext values", () => {

            });

            it("Correctly sets quoted-string values", () => {
115

116 117 118 119 120
            });

            it("Throws an exception on malformed string", () => {

            });
121 122 123
        });
    });

124 125 126 127 128 129 130 131 132 133 134 135 136
    /**
     * Tests the generation of param strings to make sure they are to spec with
     *   RFC 5545's ABNF definition (pg. 10):
     *
     *     param = param-name "=" param-value *("," param-value)
     *     ; Each property defines the specific ABNF for the parameters
     *     ; allowed on the property. Refer to specific properties for
     *     ; precise parameter ABNF.
     *
     * tl;dr of the above snippet is that this is a basic structure check so
     *   that the higher level classes can simply validate proper types and use
     *   the lower level generate method.
     */
137
    describe("***generate()", () => {
138 139 140 141 142 143 144
        it("Correctly generates single-valued parameters", () => {

        });

        it("Correctly generates double-valued parameters", () => {

        });
145

146 147 148
        it("Correctly generates multi-valued parameters", () => {

        });
149 150
    });

151 152 153 154 155 156
    /**
     * There are currently many more static methods then will be in the final
     *   class. I need to start building the other objects before I will have a
     *   better idea which of these validators are unique or need to be moved to
     *   a general utility file.
     */
157
    describe("STATIC Methods", () => {
158 159 160 161 162 163
        /**
         * iana-token definition from RFC 5545 (pg. 10)
         *
         *     iana-token = 1*(ALPHA / DIGIT / "-")
         *     ; iCalendar identifier registered with IANA
         */
164 165 166 167 168 169
        describe("isIanaToken()", () => {
            it("exists", () => {
                expect(Parameter).itself.respondsTo("isIanaToken");
            });

            it("returns true for valid iana-token", () => {
Mark Stenglein's avatar
Mark Stenglein committed
170 171 172
                const test: string = "ICALENDAR";
                const result: boolean = Parameter.isIanaToken(test);
                expect(result).to.be.true;
173 174 175
            });

            it("returns false for empty string", () => {
Mark Stenglein's avatar
Mark Stenglein committed
176 177 178
                const test: string = "";
                const result: boolean = Parameter.isIanaToken(test);
                expect(result).to.be.false;
179 180 181
            });

            it("returns true for string containing \"-\"", () => {
Mark Stenglein's avatar
Mark Stenglein committed
182 183 184
                const test: string = "ICAL-TEST";
                const result: boolean = Parameter.isIanaToken(test);
                expect(result).to.be.true;
185 186 187
            });

            it("returns false for string containing special chars", () => {
Mark Stenglein's avatar
Mark Stenglein committed
188 189 190
                const test: string = "!CAL-TEST";
                const result: boolean = Parameter.isIanaToken(test);
                expect(result).to.be.false;
191 192 193
            });

            it("returns false for string containing control chars", () => {
Mark Stenglein's avatar
Mark Stenglein committed
194 195 196
                const test: string = "CAL-TEST\r\n";
                const result: boolean = Parameter.isIanaToken(test);
                expect(result).to.be.false;
197 198 199 200
            });

        });

201 202 203 204 205 206 207 208 209
        /**
         * x-name definition from RFC 5545 (pg. 10)
         *
         *     x-name = "X-" [vendorid "-"] 1*(ALPHA / DIGIT / "-")
         *     ; Reserved for experimental use.
         *
         *     vendorid = 3*(ALPHA / DIGIT)
         *     ; Vendor identification
         */
210 211 212 213 214 215
        describe("isXName()", () => {
            it("exists", () => {
                expect(Parameter).itself.respondsTo("isXName");
            });

            it("returns true for x-name value without vendorid", () => {
Mark Stenglein's avatar
Mark Stenglein committed
216 217 218
                const test: string = "X-TESTING-TIME";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.true;
219 220 221
            });

            it("returns true for x-name value containing vendorid", () => {
Mark Stenglein's avatar
Mark Stenglein committed
222 223 224
                const test: string = "X-MIC-TESTING-TIME";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.true;
225 226 227
            });

            it("returns false for x-name containing special chars", () => {
Mark Stenglein's avatar
Mark Stenglein committed
228 229 230
                const test: string = "X-TEST!NG-TIME";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.false;
231 232 233
            });

            it("returns false for x-name containing control chars", () => {
Mark Stenglein's avatar
Mark Stenglein committed
234 235 236
                const test: string = "X-TESTNG-TIME/r/n";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.false;
237 238 239
            });

            it("returns false when missing \"X-\" from start" +
Mark Stenglein's avatar
Mark Stenglein committed
240 241 242 243
                    " of x-name", () => {
                const test: string = "TESTNG-TIME";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.false;
244 245 246
            });

            it("returns false for empty string", () => {
Mark Stenglein's avatar
Mark Stenglein committed
247 248 249
                const test: string = "";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.false;
250 251 252 253
            });

            it("returns false for vendorid containing special" +
                " chars (even \"-\")", () => {
Mark Stenglein's avatar
Mark Stenglein committed
254 255 256
                const test: string = "X-V#NDOR-TEST";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.false;
257 258 259
            });
        });

260 261 262 263
        /**
         * param-text definition from RFC 5545 (pg. 10)
         *
         *     paramtext = *SAFE-CHAR
Mark Stenglein's avatar
Mark Stenglein committed
264 265 266 267 268
         *
         * Because this only exists to check and make sure that it is safe text
         *   of any length, this really only needs to be checked to be sure it
         *   exists and somebody hasn't renamed/deleted it. It's essentially an
         *   alias.
269
         */
270
        describe("isParamText()", () => {
271 272 273 274
            it("exists", () => {
                expect(Parameter).itself.respondsTo("isParamText");
            });

Mark Stenglein's avatar
Mark Stenglein committed
275
            it("see isSafeChar() for other tests; this is an alias", () => {});
276 277
        });

278 279 280 281 282 283 284
        /**
         * SAFE-CHAR definition from RFC 5545 (pg. 11)
         *
         *     SAFE-CHAR = WSP / %x21 / %x23-2B / %x2D-39 / %x3C-7E
         *               / NON-US-ASCII
         *     ; Any character except CONTROL, DQUOTE, ";", ":", ","
         */
285
        describe("isSafeChar()", () => {
286 287 288
            it("exists", () => {
                expect(Parameter).itself.respondsTo("isSafeChar");
            });
Mark Stenglein's avatar
Mark Stenglein committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339

            it("Returns true for any char except ctl,dquote,;,:,\",\"", () => {
                const test: string = "abcdefghijklmnopqrstuvwxyz" +
                    " ABCDEFGHIJKLMNOPQRSTUVWXYZ 01234567890 !@#$%^&*()~`'.";
                const result: boolean = Parameter.isSafeChar(test);

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

            it("Returns false for \";\"", () => {
                const test: string = "test this char ';'";
                const result: boolean = Parameter.isSafeChar(test);

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

            it("Returns false for \":\"", () => {
                const test: string = "test this char ':'";
                const result: boolean = Parameter.isSafeChar(test);

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

            it("Returns false for \",\"", () => {
                const test: string = "test this char ','";
                const result: boolean = Parameter.isSafeChar(test);

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

            it("Returns false for DQUOTE", () => {
                const test: string = "test this char '\"'";
                const result: boolean = Parameter.isSafeChar(test);

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

            /**
             * This test needs to be updated for the CONTROL char. I'm
             *   not really sure how to make a CONTROL char in js escape
             *   sequences though, so I'm skipping for now.
             *
             * TODO: Come back to this!
             */
            it("Returns false for CONTROL", () => {
                const test: string = "test this char '\"'";
                const result: boolean = false; // Parameter.isSafeChar(test);

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

340 341
        });

342 343 344 345 346
        /**
         * quoted-string definition from RFC 5545 (pg. 11)
         *
         *     quoted-string = DQUOTE *QSAFE-CHAR DQUOTE
         */
347
        describe("isQuotedString()", () => {
348 349 350
            it("exists", () => {
                expect(Parameter).itself.respondsTo("isQuotedString");
            });
Mark Stenglein's avatar
Mark Stenglein committed
351

Mark Stenglein's avatar
Mark Stenglein committed
352 353 354
            it("returns true for valid quoted-string", () => {
                const test: string = "\"Test this valid quote-string!\"";
                const result: boolean = Parameter.isQuotedString(test);
Mark Stenglein's avatar
Mark Stenglein committed
355

Mark Stenglein's avatar
Mark Stenglein committed
356 357 358 359 360 361 362 363
                expect(result).to.be.true;
            });

            it("returns false for single quotes", () => {
                const test: string = "'Test this invalid quote-string!'";
                const result: boolean = Parameter.isQuotedString(test);

                expect(result).to.be.false;
Mark Stenglein's avatar
Mark Stenglein committed
364 365
            });

Mark Stenglein's avatar
Mark Stenglein committed
366 367 368 369 370 371 372
            it("returns false for invalid QSAFE-CHAR", () => {
                const test: string = "\"Test this invalid, \"FAKE NEWS,\"" +
                    " quote-string!\"";
                const result: boolean = Parameter.isQuotedString(test);

                expect(result).to.be.false;
            });
373 374
        });

375 376 377 378 379 380
        /**
         * QSAFE-CHAR definition from RFC 5545 (pg. 11)
         *
         *     QSAFE-CHAR = WSP / %x21 / %x23-7E / NON-US-ASCII
         *     ; Any character except CONTROL and DQUOTE
         */
381
        describe("isQSafeChar()", () => {
382 383 384
            it("exists", () => {
                expect(Parameter).itself.respondsTo("isQSafeChar");
            });
Mark Stenglein's avatar
Mark Stenglein committed
385

Mark Stenglein's avatar
Mark Stenglein committed
386 387 388 389 390
            it("returns true for valid QSAFE-CHAR", () => {
                const test: string = "abcdefghijklmnopqrstuvwxyz" +
                    " ABCDEFGHIJKLMNOPQRSTUVWXYZ 01234567890 !@#$%^&*()~`'." +
                    " ;:,";
                const result: boolean = Parameter.isQSafeChar(test);
Mark Stenglein's avatar
Mark Stenglein committed
391

Mark Stenglein's avatar
Mark Stenglein committed
392
                expect(result).to.be.true;
Mark Stenglein's avatar
Mark Stenglein committed
393 394
            });

Mark Stenglein's avatar
Mark Stenglein committed
395 396 397 398 399 400
            it("Returns false for DQUOTE", () => {
                const test: string = "test this char '\"'";
                const result: boolean = Parameter.isQuotedString(test);

                expect(result).to.be.false;
            });
401

Mark Stenglein's avatar
Mark Stenglein committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415
            /**
             * This test needs to be updated for the CONTROL char. I'm
             *   not really sure how to make a CONTROL char in js escape
             *   sequences though, so I'm skipping for now.
             *
             * TODO: Come back to this!
             */
            it("Returns false for CONTROL", () => {
                const test: string = "test this char '\"'";
                const result: boolean = false; // Parameter.isSafeChar(test);

                expect(result).to.be.false;
            });
        });
416 417
    });
});