From f5f9a1b5e8795230cb5ea6e1a4740f9a1c0ab163 Mon Sep 17 00:00:00 2001 From: Mikko Peltonen Date: Sat, 29 Dec 2018 19:22:37 +0200 Subject: [PATCH] Convert some specs to TypeScript --- spec/specs/_.coffee | 116 ---------------------- spec/specs/_.ts | 146 ++++++++++++++++++++++++++++ spec/specs/event.coffee | 20 ---- spec/specs/event.ts | 22 +++++ spec/specs/observable.coffee | 28 ------ spec/specs/observable.ts | 40 ++++++++ spec/specs/packagemanagement.coffee | 14 --- spec/specs/packagemanagement.ts | 16 +++ spec/specs/try.coffee | 22 ----- spec/specs/try.ts | 25 +++++ 10 files changed, 249 insertions(+), 200 deletions(-) delete mode 100644 spec/specs/_.coffee create mode 100644 spec/specs/_.ts delete mode 100644 spec/specs/event.coffee create mode 100644 spec/specs/event.ts delete mode 100644 spec/specs/observable.coffee create mode 100644 spec/specs/observable.ts delete mode 100644 spec/specs/packagemanagement.coffee create mode 100644 spec/specs/packagemanagement.ts delete mode 100644 spec/specs/try.coffee create mode 100644 spec/specs/try.ts diff --git a/spec/specs/_.coffee b/spec/specs/_.coffee deleted file mode 100644 index 7adb6f0ae..000000000 --- a/spec/specs/_.coffee +++ /dev/null @@ -1,116 +0,0 @@ -_ = require("../../dist/Bacon")._ -expect = require("chai").expect - -describe "Bacon._", -> - it "head", -> - expect(_.head([5,2,9])).to.equal(5) - expect(_.head([])).to.equal(undefined) - expect(_.head(5)).to.equal(undefined) - it "always", -> expect(_.always(5)("francis")).to.equal(5) - it "negate", -> - expect(_.negate(_.always(true))("timanttikobra")).to.be.false - it "empty", -> - expect(_.empty([])).to.be.true - expect(_.empty("")).to.be.true - expect(_.empty([1])).to.be.false - expect(_.empty("1")).to.be.false - it "tail", -> - expect(_.tail([1,2,3])).to.deep.equal([2,3]) - expect(_.tail([1])).to.deep.equal([]) - expect(_.tail([])).to.deep.equal([]) - it "filter", -> - expect(_.filter(_.empty, ["","1",[],[2]])).to.deep.equal(["",[]]) - it "map", -> - expect(_.map(_.head, [ - [], [1], [2,2], [3,3,3] - ])).to.deep.equal([ - undefined, 1, 2, 3 - ]) - it "flatMap", -> - expect(_.flatMap(((x) -> [x, x]), [1,2,3])).to.deep.equal([1,1,2,2,3,3]) - describe "each", -> - it "provides key and value to iterator", -> - expectKeyVals = (x, expectedKeys, expectedValues) -> - keys = [] - values = [] - _.each(x, (key, value) -> - keys.push(key) - values.push(value) - ) - expect([keys, values]).to.deep.equal([expectedKeys, expectedValues]) - expectKeyVals( - {cat:"furry",bird:"feathery"}, ["cat","bird"], ["furry","feathery"] - ) - expectKeyVals([1,2,3], ["0","1","2"], [1,2,3]) - describe "toArray", -> - it "works", -> - expect(_.toArray(2)).to.deep.equal([2]) - it "ignores rest of arguments", -> - expect(_.toArray(1,1,2)).to.deep.equal([1]) - it "should, when given an array, return it back (not a copy)", -> - arr = [] - expect(_.toArray(arr)).to.equal(arr) - it "indexOf", -> - expect(_.indexOf([1,2], 1)).to.equal(0) - expect(_.indexOf([1,2], 2)).to.equal(1) - expect(_.indexOf([1,2], 3)).to.equal(-1) - it "contains", -> - expect(_.contains("abc", "c")).to.be.true - expect(_.contains("abc", "x")).to.be.false - expect(_.contains([2,4,6], 4)).to.be.true - expect(_.contains([2,4,6], 3)).to.be.false - it "id", -> - obj = {} - expect(_.id(obj)).to.equal(obj) - it "last", -> - expect(_.last([2,4])).to.equal(4) - expect(_.last("last")).to.equal("t") - it "all", -> - it "works", -> - expect(_.all([ [false,true], [true,true] ], _.head)).to.be.false - expect(_.all([ [true,false], [true,true] ], _.head)).to.be.true - describe "any", -> - it "works", -> - expect(_.any([ [false,true], [true,true] ], _.head)).to.be.true - expect(_.any([ [false,false], [false,true] ], _.head)).to.be.false - it "without", -> - expect(_.without("apple", ["bacon","apple","apple","omelette"])) - .to.deep.equal(["bacon","omelette"]) - it "remove", -> - expect(_.remove("apple", ["bacon","apple","apple","omelette"])) - .to.deep.equal(["apple"]) - expect(_.remove("raisin", ["bacon","apple","apple","omelette"])) - .to.deep.equal(undefined) - it "fold", -> - expect(_.fold([1,2,3,4,5], 0, (s, n) -> s + n)).to.equal(15) - describe "toString", -> - it "for booleans", -> - expect(_.toString(true)).to.equal("true") - it "for numbers", -> - expect(_.toString(1)).to.equal("1") - expect(_.toString(1.1)).to.equal("1.1") - it "for undefined and null", -> - expect(_.toString(undefined)).to.equal("undefined") - expect(_.toString(null)).to.equal("undefined") - it "for strings", -> - expect(_.toString("lol")).to.equal("lol") - it "for dates", -> - expect(_.toString(new Date((new Date(0)).getTimezoneOffset() * 60 * 1000))).to.contain("1970") - it "for arrays", -> - expect(_.toString([1,2,3])).to.equal("[1,2,3]") - it "for objects", -> - expect(_.toString({a: "b"})).to.equal("{a:b}") - expect(_.toString({a: "b", c: "d"})).to.equal("{a:b,c:d}") - it "for circular refs", -> - obj = { name : "nasty" } - obj.self = obj - expect(_.toString(obj).length).to.be.below(100) - it "works even when enumerable properties throw errors on access", -> - obj = { "name": "madcow" } - - Object.defineProperty obj, "prop", - enumerable: true - get: -> - throw new Error "an error" - - expect(_.toString(obj)).to.equal("{name:madcow,prop:Error: an error}") diff --git a/spec/specs/_.ts b/spec/specs/_.ts new file mode 100644 index 000000000..b374897d2 --- /dev/null +++ b/spec/specs/_.ts @@ -0,0 +1,146 @@ +import { _ } from "../.."; +import { expect } from "chai"; + +describe("Bacon._", () => { + it("head", () => { + expect(_.head([5,2,9])).to.equal(5); + expect(_.head([])).to.equal(undefined); + }); + it("always", () => { + expect(_.always(5)()).to.equal(5) + }); + it("negate", () => { + expect(_.negate(_.always(true))("timanttikobra")).to.be.false + }); + it("empty", () => { + expect(_.empty([])).to.be.true; + expect(_.empty([1])).to.be.false; + }); + it("tail", () => { + expect(_.tail([1,2,3])).to.deep.equal([2,3]); + expect(_.tail([1])).to.deep.equal([]); + expect(_.tail([])).to.deep.equal([]); + }); + it("filter", () => { + expect(_.filter(_.empty, ["","1",[],[2]])).to.deep.equal(["",[]]) + }); + it("map", () => + expect(_.map(_.head, [ + [], [1], [2,2], [3,3,3] + ])).to.deep.equal([ + undefined, 1, 2, 3 + ]) + ); + it("flatMap", () => { + expect(_.flatMap((x: number) => [x, x], [1,2,3])).to.deep.equal([1,1,2,2,3,3]) + }); + describe("each", () => + it("provides key and value to iterator", function() { + const expectKeyVals = function(x: object, expectedKeys: string[], expectedValues: Array) { + const keys: string[] = []; + const values: string[] = []; + _.each(x, (key: string, value: string) => { + keys.push(key); + values.push(value); + }); + expect([keys, values]).to.deep.equal([expectedKeys, expectedValues]); + }; + expectKeyVals({ cat: "furry", bird: "feathery" }, ["cat","bird"], ["furry","feathery"]); + expectKeyVals([1,2,3], ["0","1","2"], [1,2,3]); + }) + ); + describe("toArray", () => { + it("works", () => { + expect(_.toArray(2)).to.deep.equal([2]) + }); + + it("ignores rest of arguments", () => { + expect(_.toArray(1)).to.deep.equal([1]) + }); + + it("should, when given an array, return it back (not a copy)", () => { + const arr: any[] = []; + expect(_.toArray(arr)).to.equal(arr); + }); + }); + it("indexOf", () => { + expect(_.indexOf([1,2], 1)).to.equal(0); + expect(_.indexOf([1,2], 2)).to.equal(1); + expect(_.indexOf([1,2], 3)).to.equal(-1); + }); + it("contains", () => { + expect(_.contains([2,4,6], 4)).to.be.true; + expect(_.contains([2,4,6], 3)).to.be.false; + expect(_.contains) + }); + it("id", () => { + const obj = {}; + expect(_.id(obj)).to.equal(obj); + }); + it("last", () => { + expect(_.last([2,4])).to.equal(4) + }); + it("all", () => + it("works", () => { + expect(_.all([ [false,true], [true,true] ], _.head)).to.be.false; + expect(_.all([ [true,false], [true,true] ], _.head)).to.be.true; + }) + ); + describe("any", () => + it("works", () => { + expect(_.any([ [false,true], [true,true] ], _.head)).to.be.true; + expect(_.any([ [false,false], [false,true] ], _.head)).to.be.false; + }) + ); + it("without", () => { + expect(_.without("apple", ["bacon","apple","apple","omelette"])).to.deep.equal(["bacon","omelette"]) + }); + it("remove", () => { + expect(_.remove("apple", ["bacon","apple","apple","omelette"])).to.deep.equal(["apple"]); + expect(_.remove("raisin", ["bacon","apple","apple","omelette"])).to.deep.equal(undefined); + }); + it("fold", () => { + expect(_.fold([1,2,3,4,5], 0, (s: number, n: number) => s + n)).to.equal(15) + }); + describe("toString", () => { + it("for booleans", () => { + expect(_.toString(true)).to.equal("true") + }); + it("for strings", () => { + expect(_.toString("lol")).to.equal("lol") + }); + it("for dates", () => { + expect(_.toString(new Date((new Date(0)).getTimezoneOffset() * 60 * 1000))).to.contain("1970") + }); + it("for arrays", () => { + expect(_.toString([1,2,3])).to.equal("[1,2,3]") + }); + it("for numbers", () => { + expect(_.toString(1)).to.equal("1"); + expect(_.toString(1.1)).to.equal("1.1"); + }); + it("for undefined and null", () => { + expect(_.toString(undefined)).to.equal("undefined"); + expect(_.toString(null)).to.equal("undefined"); + }); + it("for objects", () => { + expect(_.toString({a: "b"})).to.equal("{a:b}"); + expect(_.toString({a: "b", c: "d"})).to.equal("{a:b,c:d}"); + }); + it("for circular refs", () => { + const obj = { name : "nasty", self: {}}; + obj.self = obj; + expect(_.toString(obj).length).to.be.below(100); + }); + it("works even when enumerable properties throw errors on access", () => { + const obj = { "name": "madcow" }; + Object.defineProperty(obj, "prop", { + enumerable: true, + get() { + throw new Error("an error"); + } + }); + expect(_.toString(obj)).to.equal("{name:madcow,prop:Error: an error}"); + }); + }); +}); diff --git a/spec/specs/event.coffee b/spec/specs/event.coffee deleted file mode 100644 index 6c1f072a5..000000000 --- a/spec/specs/event.coffee +++ /dev/null @@ -1,20 +0,0 @@ -Bacon = require("../../dist/Bacon") -expect = require("chai").expect - -describe "Bacon.Event", -> - describe "String presentations", -> - describe "Initial(1).toString", -> - it "is 1", -> - expect(new Bacon.Initial(1).toString()).to.equal("1") - describe "Next({a:1i}).toString", -> - it "is {a:1}", -> - expect(new Bacon.Next({a:1}).toString()).to.equal("{a:1}") - describe "Error({a:1}).toString", -> - it "is {a:1}", -> - expect(new Bacon.Error({a:1}).toString()).to.equal(" {a:1}") - describe "End.toString", -> - it "is ", -> - expect(new Bacon.End().toString()).to.equal("") - describe "inspect", -> - it "is the same as toString", -> - expect(new Bacon.Initial(1).inspect()).to.equal("1") diff --git a/spec/specs/event.ts b/spec/specs/event.ts new file mode 100644 index 000000000..1abf5ab0c --- /dev/null +++ b/spec/specs/event.ts @@ -0,0 +1,22 @@ +import * as Bacon from "../.."; +import { expect } from "chai"; + +describe("Bacon.Event", () => + describe("String presentations", () => { + describe("Initial(1).toString", () => + it("is 1", () => expect(new Bacon.Initial(1).toString()).to.equal("1")) + ); + describe("Next({a:1i}).toString", () => + it("is {a:1}", () => expect(new Bacon.Next({a:1}).toString()).to.equal("{a:1}")) + ); + describe("Error({a:1}).toString", () => + it("is {a:1}", () => expect(new Bacon.Error({a:1}).toString()).to.equal(" {a:1}")) + ); + describe("End.toString", () => + it("is ", () => expect(new Bacon.End().toString()).to.equal("")) + ); + describe("inspect", () => + it("is the same as toString", () => expect(new Bacon.Initial(1).inspect()).to.equal("1")) + ); + }) +); diff --git a/spec/specs/observable.coffee b/spec/specs/observable.coffee deleted file mode 100644 index 421a1d25c..000000000 --- a/spec/specs/observable.coffee +++ /dev/null @@ -1,28 +0,0 @@ -Bacon = require("../../dist/Bacon") -expect = require("chai").expect - -describe "Observable::onEnd", -> - it "is called on stream end", -> - s = new Bacon.Bus() - ended = false - s.onEnd(-> ended = true) - s.push("LOL") - expect(ended).to.deep.equal(false) - s.end() - expect(ended).to.deep.equal(true) - -describe "Meta-info", -> - obs = Bacon.once(1).map(->) - describe "Observable::desc", -> - it "returns structured description", -> - expect(obs.desc.method).to.equal("map") - - describe "Observable::deps", -> - it "returns dependencies", -> - expect(obs.deps().length).to.equal(1) - expect(obs.deps()[0].toString()).to.equal("Bacon.once(1)") - - describe "Observable::internalDeps", -> - it "returns \"real\" deps", -> - expect(obs.deps().length).to.equal(1) - expect(obs.deps()[0].toString()).to.equal("Bacon.once(1)") diff --git a/spec/specs/observable.ts b/spec/specs/observable.ts new file mode 100644 index 000000000..0320af250 --- /dev/null +++ b/spec/specs/observable.ts @@ -0,0 +1,40 @@ +import * as Bacon from "../.."; +import { expect } from "chai"; + +describe("Observable::onEnd", () => + it("is called on stream end", () => { + const s = new Bacon.Bus(); + let ended = false; + s.onEnd(() => { + ended = true; + return undefined; + }); + s.push("LOL"); + expect(ended).to.deep.equal(false); + s.end(); + expect(ended).to.deep.equal(true); + }) +); + +describe("Meta-info", function() { + const obs = Bacon.once(1).map(function() {}); + describe("Observable::desc", () => + it("returns structured description", () => { + expect(obs.desc.method).to.equal("map") + }) + ); + + describe("Observable::deps", () => + it("returns dependencies", function() { + expect(obs.deps().length).to.equal(1); + expect(obs.deps()[0].toString()).to.equal("Bacon.once(1)"); + }) + ); + + describe("Observable::internalDeps", () => + it("returns \"real\" deps", function() { + expect(obs.deps().length).to.equal(1); + expect(obs.deps()[0].toString()).to.equal("Bacon.once(1)"); + }) + ); +}); diff --git a/spec/specs/packagemanagement.coffee b/spec/specs/packagemanagement.coffee deleted file mode 100644 index 069a5c4bc..000000000 --- a/spec/specs/packagemanagement.coffee +++ /dev/null @@ -1,14 +0,0 @@ -describe "Package management", -> - return unless typeof window == 'undefined' - - fs = require("fs") - verifyJson = (fn) -> - JSON.parse(fs.readFileSync("bower.json", "utf-8")) - it "NPM", -> - verifyJson "package.json" - it "Component", -> - verifyJson "component.json" - it "Bower", -> - bowerJson = require('bower-json') - json = verifyJson "bower.json" - bowerJson.validate(json) diff --git a/spec/specs/packagemanagement.ts b/spec/specs/packagemanagement.ts new file mode 100644 index 000000000..7a099c5b3 --- /dev/null +++ b/spec/specs/packagemanagement.ts @@ -0,0 +1,16 @@ +describe("Package management", function() { + if (typeof window !== 'undefined') { return; } + + const fs = require("fs"); + const verifyJson = (fn: string) => + JSON.parse(fs.readFileSync(fn, "utf-8")); + it("NPM", () => + verifyJson("package.json")); + it("Component", () => + verifyJson("component.json")); + it("Bower", () => { + const bowerJson = require('bower-json'); + const json = verifyJson("bower.json"); + bowerJson.validate(json); + }); +}); diff --git a/spec/specs/try.coffee b/spec/specs/try.coffee deleted file mode 100644 index a85cc9ad4..000000000 --- a/spec/specs/try.coffee +++ /dev/null @@ -1,22 +0,0 @@ -Bacon = require("../../dist/Bacon") -expect = require("chai").expect - -describe "Bacon.try", -> - it "maps the value over the function", -> - Bacon - .once('{"valid json": true}') - .flatMap(Bacon.try(JSON.parse)) - .doError(() -> throw "Bacon.try test failed") - .onValue(() -> "all ok") - - it "returns a Bacon.Error if the mapper function throws an exception", -> - Bacon - .once('{"invalid json: true}') - .flatMap(Bacon.try(JSON.parse)) - .doAction(() -> throw "Bacon.try test failed") - .onError((err) -> - if err instanceof SyntaxError - "all ok" - else - throw "Bacon.try did not emit an expected error event" - ) diff --git a/spec/specs/try.ts b/spec/specs/try.ts new file mode 100644 index 000000000..5e01d4c19 --- /dev/null +++ b/spec/specs/try.ts @@ -0,0 +1,25 @@ +import * as Bacon from "../.."; + +describe("Bacon.try", () => { + it("maps the value over the function", () => + Bacon + .once('{"valid json": true}') + .flatMap(Bacon.try(JSON.parse)) + .doError(() => { throw "Bacon.try test failed"; }) + .onValue(() => Bacon.noMore) + ); + + return it("returns a Bacon.Error if the mapper function throws an exception", () => + Bacon + .once('{"invalid json: true}') + .flatMap(Bacon.try(JSON.parse)) + .doAction(() => { throw "Bacon.try test failed"; }) + .onError((err) => { + if (err instanceof SyntaxError) { + return Bacon.noMore; + } else { + throw "Bacon.try did not emit an expected error event"; + } + }) + ); +});