aboutsummaryrefslogtreecommitdiff
path: root/src/formats.test.ts
blob: 1536b5db2ad4e2c3a7c29979dd298ea6e06cd8d4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import Formats, { defaultColorMatches } from "./formats";
import fs from "fs";
import path from "path";

describe("Color matcher", () => {
  it.each`
    n      | s1     | s2    | expected
    ${100} | ${75}  | ${50} | ${"4c1"}
    ${75}  | ${75}  | ${50} | ${"4c1"}
    ${50}  | ${75}  | ${50} | ${"ec1"}
    ${0}   | ${75}  | ${50} | ${"e11"}
    ${51}  | ${51}  | ${50} | ${"4c1"}
    ${50}  | ${51}  | ${50} | ${"ec1"}
    ${0}   | ${50}  | ${1}  | ${"e11"}
    ${100} | ${100} | ${0}  | ${"4c1"}
    ${0}   | ${100} | ${0}  | ${"ec1"}
    ${70}  | ${70}  | ${60} | ${"4c1"}
    ${69}  | ${70}  | ${60} | ${"5c1"}
    ${68}  | ${70}  | ${60} | ${"6c1"}
    ${67}  | ${70}  | ${60} | ${"7c1"}
    ${66}  | ${70}  | ${60} | ${"8c1"}
    ${65}  | ${70}  | ${60} | ${"9c1"}
    ${64}  | ${70}  | ${60} | ${"ac1"}
    ${63}  | ${70}  | ${60} | ${"bc1"}
    ${62}  | ${70}  | ${60} | ${"cc1"}
    ${61}  | ${70}  | ${60} | ${"dc1"}
    ${60}  | ${70}  | ${60} | ${"ec1"}
    ${11}  | ${75}  | ${11} | ${"ec1"}
    ${10}  | ${75}  | ${11} | ${"eb1"}
    ${9}   | ${75}  | ${11} | ${"ea1"}
    ${8}   | ${75}  | ${11} | ${"e91"}
    ${7}   | ${75}  | ${11} | ${"e81"}
    ${6}   | ${75}  | ${11} | ${"e71"}
    ${5}   | ${75}  | ${11} | ${"e61"}
    ${4}   | ${75}  | ${11} | ${"e51"}
    ${3}   | ${75}  | ${11} | ${"e41"}
    ${2}   | ${75}  | ${11} | ${"e31"}
    ${1}   | ${75}  | ${11} | ${"e21"}
    ${0}   | ${75}  | ${11} | ${"e11"}
    ${51}  | ${51}  | ${11} | ${"4c1"}
    ${50}  | ${51}  | ${11} | ${"4c1"}
    ${49}  | ${51}  | ${11} | ${"4c1"}
    ${48}  | ${51}  | ${11} | ${"4c1"}
    ${47}  | ${51}  | ${11} | ${"5c1"}
    ${46}  | ${51}  | ${11} | ${"5c1"}
    ${45}  | ${51}  | ${11} | ${"5c1"}
    ${44}  | ${51}  | ${11} | ${"5c1"}
    ${43}  | ${51}  | ${11} | ${"6c1"}
    ${39}  | ${51}  | ${11} | ${"7c1"}
    ${35}  | ${51}  | ${11} | ${"8c1"}
    ${31}  | ${51}  | ${11} | ${"9c1"}
    ${27}  | ${51}  | ${11} | ${"ac1"}
    ${23}  | ${51}  | ${11} | ${"bc1"}
    ${19}  | ${51}  | ${11} | ${"cc1"}
    ${15}  | ${51}  | ${11} | ${"dc1"}
    ${11}  | ${51}  | ${11} | ${"ec1"}
  `("should return $expected at $n%", ({ n, s1, s2, expected }) => {
    // Arrange
    const gradient = { stage1: s1, stage2: s2 };

    // Act
    const result = defaultColorMatches(n, gradient);

    // Assert
    expect(result).toEqual(expected);
  });
});

describe("Formats object", () => {
  it("should list the available formats", () => {
    // Arrange

    // Act
    const result = Formats.listFormats();

    // Assert
    expect(result).toEqual(["tarpaulin"]);
  });

  it("should return the requested format", () => {
    // Arrange

    // Act
    const result = Formats.getFormat("tarpaulin");

    // Assert
    expect(result).toBeDefined();
    expect(result.matchColor).toBeInstanceOf(Function);
    expect(result.parseCoverage).toBeInstanceOf(Function);
  });
});

describe("Tarpaulin format", () => {
  const reportPath = (file: string): string =>
    path.join(__dirname, "..", "example_reports", file);

  it("should use the default color matcher", () => {
    // Arrange
    const format = Formats.getFormat("tarpaulin");

    // Act
    const matcher = format.matchColor;

    // Assert
    expect(matcher).toEqual(defaultColorMatches);
  });

  it("should parse coverage from a normal tarpaulin file", () => {
    // Arrange
    const file = fs.readFileSync(reportPath("tarpaulin-report.html"), "utf-8");

    const format = Formats.getFormat("tarpaulin");

    // Act
    const result = format.parseCoverage(file);

    // Assert
    expect(typeof result).toEqual("number");
    if (typeof result === "number") {
      // 96.17% is the result given in the document itself
      expect(result.toFixed(2)).toEqual("96.17");
    }
  });

  it("should parse coverage from an empty tarpaulin file", () => {
    // Arrange
    const file = fs.readFileSync(reportPath("tarpaulin-empty.html"), "utf-8");

    const format = Formats.getFormat("tarpaulin");

    // Act
    const result = format.parseCoverage(file);

    // Assert
    expect(typeof result).toEqual("number");
    if (typeof result === "number") {
      expect(result.toFixed(2)).toEqual("0.00");
    }
  });

  it("should return error when parsing coverage from invalid file", () => {
    // Arrange
    const file = fs.readFileSync(reportPath("tarpaulin-invalid.html"), "utf-8");

    const format = Formats.getFormat("tarpaulin");

    // Act
    const result = format.parseCoverage(file);

    // Assert
    expect(typeof result).not.toEqual("number");
    if (typeof result !== "number") {
      expect(result.message).toEqual("Invalid report document");
    }
  });
});