Skip to content

Commit

Permalink
test: simplify testing
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed Mar 7, 2019
1 parent 8956db1 commit 4672638
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 99 deletions.
86 changes: 22 additions & 64 deletions __tests__/Ratio_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,28 @@ open Jest;
open Expect;

testAll(
"colors are equal",
"color contrasts",
[
("#ffffff", "#ffffff"),
("ffffff", "ffffff"),
("rgb(255, 255, 255)", "rgb(255, 255, 255)"),
("rgb(255,255,255)", "rgb(255, 255, 255)"),
("hsl(255, 30%, 40%)", "hsl(255, 30%, 40%)"),
("hsl(255,30,40)", "hsl(255, 30%, 40%)"),
("#ffffff", "rgb(255, 255, 255)"),
("#ffffff", "#ffffff", 1.0),
("ffffff", "ffffff", 1.0),
("rgb(255, 255, 255)", "rgb(255, 255, 255)", 1.0),
("rgb(255,255,255)", "rgb(255, 255, 255)", 1.0),
("hsl(255, 30%, 40%)", "hsl(255, 30%, 40%)", 1.0),
("hsl(255,30,40)", "hsl(255, 30%, 40%)", 1.0),
("#ffffff", "rgb(255, 255, 255)", 1.0),
("#ffffff", "#000000", 21.0),
("#ffffff", "#777777", 4.48),
("#0088FF", "#C611AB", 1.47),
("ffffff", "777777", 4.48),
("fff", "777", 4.48),
("08f", "fff", 3.52),
("rgb(255,255,255)", "#777777", 4.48),
("rgb(255,255,255)", "rgb(77,77,77)", 8.45),
("hsl(0, 0%, 20%)", "#ffffff", 12.63),
("hsl(210, 30%, 48%)", "#ffffff", 4.47),
("hsl(210, 30%, 68%)", "#ffffff", 2.31),
("hsl(0, 0%, 20%)", "hsl(0, 0%, 100%)", 12.63),
],
((fg, bg)) =>
expect(Ratio.calculate(fg, bg)) |> toEqual(1.0)
((fg, bg, expected)) =>
expect(Ratio.calculate(fg, bg)) |> toEqual(expected)
);

describe("#calculate", () => {
test("handles white on black", () =>
expect(Ratio.calculate("#ffffff", "#000000")) |> toEqual(21.0)
);

test("handles white on grayish", () =>
expect(Ratio.calculate("#ffffff", "#777777")) |> toEqual(4.48)
);

test("handles random colors (blue on pink)", () =>
expect(Ratio.calculate("#0088FF", "#C611AB")) |> toEqual(1.47)
);

test("handles hex without hash", () =>
expect(Ratio.calculate("ffffff", "777777")) |> toEqual(4.48)
);

test("handles hex with shorthand", () =>
expect(Ratio.calculate("fff", "777")) |> toEqual(4.48)
);

test("handles hex with non-uniform shorthand", () =>
expect(Ratio.calculate("08f", "fff")) |> toEqual(3.52)
);

test("handles rgb colors", () =>
expect(Ratio.calculate("rgb(255,255,255)", "#777777")) |> toEqual(4.48)
);

test("handles two rgb colors", () =>
expect(Ratio.calculate("rgb(255,255,255)", "rgb(77,77,77)"))
|> toEqual(8.45)
);

test("handles hsl colors without saturation", () =>
expect(Ratio.calculate("hsl(0, 0%, 20%)", "#ffffff")) |> toEqual(12.63)
);

test("handles hsl colors with lightness lower than 50", () =>
expect(Ratio.calculate("hsl(210, 30%, 48%)", "#ffffff"))
|> toEqual(4.47)
);

test("handles hsl colors with lightness higher than 50", () =>
expect(Ratio.calculate("hsl(210, 30%, 68%)", "#ffffff"))
|> toEqual(2.31)
);

test("handles two hsl colors", () =>
expect(Ratio.calculate("hsl(0, 0%, 20%)", "hsl(0, 0%, 100%)"))
|> toEqual(12.63)
);
});
53 changes: 18 additions & 35 deletions __tests__/Score_test.re
Original file line number Diff line number Diff line change
@@ -1,38 +1,21 @@
open Jest;
open Expect;

describe("#calculateFromRatio", () => {
test("handles AAA", () =>
expect(Score.calculateFromRatio(7.1)) |> toEqual("AAA")
);

test("handles AA", () =>
expect(Score.calculateFromRatio(4.6)) |> toEqual("AA")
);

test("handles AA Large", () =>
expect(Score.calculateFromRatio(3.9)) |> toEqual("AA Large")
);

test("handles white on grayish", () =>
expect(Score.calculateFromRatio(2.9)) |> toEqual("Fail")
);
});

describe("#calculate", () => {
test("handles AAA", () =>
expect(Score.calculate("#ffffff", "#000000")) |> toEqual("AAA")
);

test("handles AA", () =>
expect(Score.calculate("#ffffff", "#666666")) |> toEqual("AA")
);

test("handles AA Large", () =>
expect(Score.calculate("#ffffff", "#888888")) |> toEqual("AA Large")
);

test("handles white on grayish", () =>
expect(Score.calculate("#ffffff", "#cccccc")) |> toEqual("Fail")
);
});
testAll(
"#calculateFromRatio",
[(7.1, "AAA"), (4.6, "AA"), (3.9, "AA Large"), (2.9, "Fail")],
((ratio, score)) =>
expect(Score.calculateFromRatio(ratio)) |> toEqual(score)
);

testAll(
"#calculate",
[
("#ffffff", "#000000", "AAA"),
("#ffffff", "#666666", "AA"),
("#ffffff", "#888888", "AA Large"),
("#ffffff", "#cccccc", "Fail"),
],
((foreground, background, score)) =>
expect(Score.calculate(foreground, background)) |> toEqual(score)
);

0 comments on commit 4672638

Please # to comment.