From 4672638da3bdf1230da01b6ca6472e6ac05f5c15 Mon Sep 17 00:00:00 2001 From: believer Date: Thu, 7 Mar 2019 19:13:42 +0100 Subject: [PATCH] test: simplify testing --- __tests__/Ratio_test.re | 86 +++++++++++------------------------------ __tests__/Score_test.re | 53 +++++++++---------------- 2 files changed, 40 insertions(+), 99 deletions(-) diff --git a/__tests__/Ratio_test.re b/__tests__/Ratio_test.re index f0eae06..939c491 100644 --- a/__tests__/Ratio_test.re +++ b/__tests__/Ratio_test.re @@ -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) - ); -}); diff --git a/__tests__/Score_test.re b/__tests__/Score_test.re index 2eeb777..a0892e7 100644 --- a/__tests__/Score_test.re +++ b/__tests__/Score_test.re @@ -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) +);