Commit b001f9be authored by Mark Stenglein's avatar Mark Stenglein

Add AltRepParam class and needed changes

- needed to change the other classes to support the AltRep and other
  classes to come.

- Also added test cases to cover the new class
parent 9279da99
Pipeline #955 passed with stage
in 24 seconds
import Parameter from "./Parameter";
/**
* Implements the Alternate text representation parameter
*
* Format Definition: This property parameter is defined by the
* following notation:
*
* altrepparam = "ALTREP" "=" DQUOTE uri DQUOTE
*
* This parameter specifies a URI that points to an alternate representation
* for a textual property value.
*
* A property specifying this parameter MUST also include a value that reflects
* the default representation of the text value.
*
* The URI parameter value MUST be specified in a quoted-string.
*
* @author Mark Stenglein <mark@stengle.in>
*/
export default class AltRepParam extends Parameter {
private _uri: string;
constructor(uri: string) {
if (!uri) {
throw new TypeError("uri must be defined for AltRepParam");
}
else {
super("ALTREP", []);
this.uri = uri;
this.reqContentValue = true;
}
}
get uri(): string {
return this._uri;
}
/**
* Allows you to set the parameter with either a quoted-string or a
* QSAFE-CHAR, which will be converted into a quoted-string
*
* @author Mark Stenglein <mark@stengle.in>
* @since 0.1.0
* @param newUri string value of the new URI which is to be validated
* @returns void
* @access public
*/
set uri(newUri: string) {
if (Parameter.isQSafeChar(newUri)) {
this._uri = newUri;
this.paramValues = ["\"" + this.uri + "\""];
}
else if (Parameter.isQuotedString(newUri)) {
this._uri = newUri;
this.paramValues = [newUri];
}
else {
throw new TypeError("URI must be QSAFE-CHAR or quoted-string");
}
}
}
......@@ -16,8 +16,27 @@ import { ICalElement } from "./ICalElement";
* @author Mark Stenglein <mark@stengle.in>
*/
export default class Parameter implements ICalElement {
/**
* Stores the parameter name. This should be set through the set
* method (which validates the input), and not by directly accessing it.
*/
private _paramName: string;
/**
* Stores the array of parameter values. This should be set through the set
* method (which validates the input), and not by directly accessing it.
*/
private _paramValues: string[];
/**
* Value determines whether or not the parameter requres that the content
* line stores a value alongside the Parameter. Consult the individual
* implementations' documentation in the RFC to see more information on
* parameters which require this.
*
* @type boolean
* @access private
* @default false
*/
private _reqContentValue: boolean = false;
/**
* Constructor builds the Parameter from the parameter name and an array of
......@@ -38,19 +57,24 @@ export default class Parameter implements ICalElement {
}
/** Get Methods */
/* Get Methods */
get paramName() {
get paramName(): string {
return this._paramName;
}
get paramValues() {
get paramValues(): string[] {
return this._paramValues;
}
/** Set Methods */
get reqContentValue(): boolean {
return this._reqContentValue;
}
/* Set Methods */
/**
* Validates input param names and saves them to the object.
......@@ -99,7 +123,7 @@ export default class Parameter implements ICalElement {
) {
valid = false;
}
})
});
if (!valid) {
throw new TypeError(
......@@ -113,6 +137,20 @@ export default class Parameter implements ICalElement {
}
/**
* Passes through and sets the reqContentValue property.
*
* @author Mark Stenglein <mark@stengle.in>
* @since 0.1.0
* @access public
* @param required boolean value to show if the param needs a content value
* @returns void
*/
set reqContentValue(required: boolean) {
this._reqContentValue = required;
}
/**
* Generates the proper string representation for a Parameter as defined by
* RFC 5545:
......
import AltRepParam from "../src/AltRepParam";
import { expect } from "chai";
import "mocha";
describe("AltRepParam", () => {
it("exists", () => {
expect(AltRepParam).to.exist;
});
describe("constructor", () => {
it("should create an object", () => {
const param: AltRepParam = new AltRepParam("valid@uri.com");
expect(param).to.exist;
});
});
describe("GET methods", () => {
describe("uri()", () => {
it("should not fail for no reason", () => {
const param: AltRepParam = new AltRepParam("valid@uri.com");
expect(param.uri).to.equal("valid@uri.com");
});
});
});
describe("SET methods", () => {
describe("uri()", () => {
it("should work for quoted-string", () => {
const param: AltRepParam = new AltRepParam("\"valid@uri.com\"");
expect(param.uri).to.equal("\"valid@uri.com\"");
});
it("should work for QSAFE-CHAR", () => {
const param: AltRepParam = new AltRepParam("valid@uri.com");
expect(param.uri).to.equal("valid@uri.com");
});
it("should throw exception for invalid uri", () => {
expect(() => {
const param: AltRepParam = new AltRepParam("val\"id@uri.com");
}).to.throw("URI must be QSAFE-CHAR or quoted-string");
});
});
});
describe("generate()", () => {
it("should generate a valid parameter", () => {
const param: AltRepParam = new AltRepParam("valid@uri.com");
const result: string = param.generate();
const expected: string = "ALTREP=\"valid@uri.com\"";
expect(result).to.equal(expected);
});
});
});
\ No newline at end of file
......@@ -16,7 +16,7 @@ describe("ContentLine", () => {
*/
describe("constructor", () => {
it("Should create an object", () => {
let param: Parameter = new Parameter("testParam", ["test1"]);
const param: Parameter = new Parameter("testParam", ["test1"]);
const test: ContentLine = new ContentLine("name", [param], "value");
let result: boolean = true;
......@@ -73,68 +73,68 @@ describe("ContentLine", () => {
});
}); /* describe("constructor") */
/**
* Test `fold` static method
*
* @author Mark Stenglein <mark@stenle.in>
*/
describe("fold", () => {
it("Should exist as a static method", () => {
expect(ContentLine).itself.respondsTo("fold");
});
/**
* 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);
it("Should not fold short lines", () => {
const test: string = "A short line";
const result: string = ContentLine.fold(test);
expect(test).to.be.equal(result);
});
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" +
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" +
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);
const result: string = ContentLine.fold(test);
expect(result).to.be.equal(expected);
});
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" +
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" +
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.";
const result: string = ContentLine.fold(test);
const result: string = ContentLine.fold(test);
expect(result).to.be.equal(expected);
});
}); /** describe("fold") */
expect(result).to.be.equal(expected);
});
}); /** describe("fold") */
describe("generate", () => {
it("Should respond as non-static method", () => {
describe("generate", () => {
it("Should respond as non-static method", () => {
expect(ContentLine).respondsTo("generate");
});
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") */
});
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") */
}); /** describe("ContentLine") */
......@@ -30,6 +30,7 @@ describe("Parameter", () => {
expect(testParam).to.have.property("_paramValues");
expect(testParam).to.have.property("paramName");
expect(testParam).to.have.property("paramValues");
expect(testParam).to.have.property("reqContentValue");
expect(testParam).to.have.property("generate");
});
});
......@@ -44,7 +45,8 @@ describe("Parameter", () => {
it("Should return the correct name", () => {
const testName: string = "TEST-NAME";
const testValues: string[] = ["value1", "value2"];
const testParam: Parameter = new Parameter(testName, testValues);
const testParam: Parameter = new Parameter(
testName, testValues);
const result: string = testParam.paramName;
expect(result).to.equal(testName);
......@@ -55,7 +57,8 @@ describe("Parameter", () => {
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 testParam: Parameter = new Parameter(
testName, testValues);
const result: string[] = testParam.paramValues;
expect(result).to.deep.equal(testValues);
......@@ -158,7 +161,7 @@ describe("Parameter", () => {
* that the higher level classes can simply validate proper types and use
* the lower level generate method.
*/
describe("***generate()", () => {
describe("generate()", () => {
it("Correctly generates single-valued parameters", () => {
const param: Parameter = new Parameter("NAME", ["value1"]);
const result: string = param.generate();
......@@ -168,7 +171,8 @@ describe("Parameter", () => {
});
it("Correctly generates double-valued parameters", () => {
const param: Parameter = new Parameter("NAME", ["value1", "value2"]);
const param: Parameter = new Parameter(
"NAME", ["value1", "value2"]);
const result: string = param.generate();
const expected: string = "NAME=value1,value2";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment