Skip to content

Commit

Permalink
feat(skip): Add skip function to Test
Browse files Browse the repository at this point in the history
  • Loading branch information
brekk committed Jul 3, 2024
1 parent 7f8b33f commit 20a2f94
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 32 deletions.
1 change: 1 addition & 0 deletions prelude/__internal__/IO.mad
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export colortrace = (fn, v, a) => trace(prettyCase(fn, v), a)

export red = Terminal.text.red
export green = Terminal.text.green
export blue = Terminal.text.blue
export yellow = Terminal.text.yellow
export grey = Terminal.text.white

Expand Down
21 changes: 5 additions & 16 deletions prelude/__internal__/Terminal.mad
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import String from "String"



type HandlerId = HandlerId
export type HandlerId

Expand All @@ -24,7 +26,6 @@ export ansi = {
FGMagenta: "35",
FGCyan: "36",
FGWhite: "37",

FGBrightBlack: "90",
FGBrightRed: "91",
FGBrightGreen: "92",
Expand All @@ -33,7 +34,6 @@ export ansi = {
FGBrightMagenta: "95",
FGBrightCyan: "96",
FGBrightWhite: "97",

BGBlack: "40",
BGRed: "41",
BGGreen: "42",
Expand All @@ -42,7 +42,6 @@ export ansi = {
BGMagenta: "45",
BGCyan: "46",
BGWhite: "47",

BGBrightBlack: "100",
BGBrightRed: "101",
BGBrightGreen: "102",
Expand All @@ -51,12 +50,10 @@ export ansi = {
BGBrightMagenta: "105",
BGBrightCyan: "106",
BGBrightWhite: "107",

FormatUnderline: "4",
FormatNoUnderline: "24",
FormatBold: "1",
FormatNoBold: "21",

FormatInvert: "7",
}

Expand All @@ -69,10 +66,7 @@ export ansi = {
* ansiColor([ansi.FormatBold, ansi.FGBrightRed], "will be red and bold")
*/
ansiColor :: List String -> String -> String
export ansiColor = (parts, str) => colorize(
`\x1b[${String.join(";", parts)}m`,
str
)
export ansiColor = (parts, str) => colorize(`\x1b[${String.join(";", parts)}m`, str)

export text = {
black: ansiColor([ansi.FGBlack]),
Expand Down Expand Up @@ -161,9 +155,7 @@ onWindowResizedFFI :: ({} -> {}) -> HandlerId
onWindowResizedFFI = extern "madlib__stdio__onWindowResized"

onWindowResized :: (#[Integer, Integer] -> {}) -> HandlerId
export onWindowResized = (cb) => onWindowResizedFFI(() => {
cb(getWindowSize())
})
export onWindowResized = (cb) => onWindowResizedFFI(() => { cb(getWindowSize()) })


clearWindowResizeHandler :: HandlerId -> {}
Expand Down Expand Up @@ -221,10 +213,7 @@ export clearKeyPressedHandler = (id) => #- {


getWindowSize :: {} -> #[Integer, Integer]
export getWindowSize = () => #[
#- process.stdout.columns -#,
#- process.stdout.rows -#,
]
export getWindowSize = () => #[#- process.stdout.columns -#, #- process.stdout.rows -#]


getTTYMode :: {} -> TTYMode
Expand Down
65 changes: 49 additions & 16 deletions prelude/__internal__/Test.mad
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ PREFIX_FAIL = IS_COLOR_ENABLED
CHAR_CROSS :: String
CHAR_CROSS = "×"

CHAR_INCOMPLETE :: String
CHAR_INCOMPLETE = "⧜"

EMPTY_REPORT :: TestReport
EMPTY_REPORT = TestReport("", 0, 0, 0)
EMPTY_REPORT = TestReport("", 0, 0, 0, 0)

CWD :: String
CWD = Process.getCurrentWorkingDirectory()
Expand All @@ -60,7 +63,7 @@ CWD = Process.getCurrentWorkingDirectory()
// SuiteResult(total, succeeded, failed)
type SuiteResult = SuiteResult(Integer, Integer, Integer)

type TestResult = Success(String) | Failure(String, String)
type TestResult = Success(String) | Failure(String, String) | Skipped(String)

isSuccess :: TestResult -> Boolean
isSuccess = (res) => where(res) {
Expand Down Expand Up @@ -104,47 +107,60 @@ export type AssertionError
| NotImplemented


// Contains the accumulated String to be displayed,
// the amount of tests run, the amount of successful tests,
// and the amount of failed tests
type TestReport = TestReport(String, Integer, Integer, Integer)
/**
* TestReport
* accumulated report - String
* total tests - Integer
* successful runs - Integer
* failed runs- Integer
* skipped - Integer
*/
type TestReport = TestReport(String, Integer, Integer, Integer, Integer)


getMessage :: TestReport -> String
getMessage = where {
TestReport(str, _, _, _) =>
TestReport(str, _, _, _, _) =>
str
}


getTotal :: TestReport -> Integer
getTotal = where {
TestReport(_, total, _, _) =>
TestReport(_, total, _, _, _) =>
total
}


getSuccessCount :: TestReport -> Integer
getSuccessCount = where {
TestReport(_, _, success, _) =>
TestReport(_, _, success, _, _) =>
success
}


getFailureCount :: TestReport -> Integer
getFailureCount = where {
TestReport(_, _, _, failed) =>
TestReport(_, _, _, failed, _) =>
failed
}

getSkippedCount :: TestReport -> Integer
getSkippedCount = where {
TestReport(_, _, _, _, skipped) =>
skipped
}

resultToReport :: TestResult -> TestReport
resultToReport = (result) => where(result) {
Failure(_, message) =>
TestReport(message, 1, 0, 1)
TestReport(message, 1, 0, 1, 0)

Success(_) =>
TestReport("", 1, 1, 0)
TestReport("", 1, 1, 0, 0)

Skipped(_) =>
TestReport("", 1, 0, 0, 1)
}


Expand All @@ -154,6 +170,7 @@ mergeReports = (t1, t2) => TestReport(
getTotal(t1) + getTotal(t2),
getSuccessCount(t1) + getSuccessCount(t2),
getFailureCount(t1) + getFailureCount(t2),
getSkippedCount(t1) + getSkippedCount(t2),
)


Expand Down Expand Up @@ -200,10 +217,11 @@ renderAssertionError = (description, assertionError) => where(assertionError) {
IS_COLOR_ENABLED ? IO.red(`${CHAR_CROSS} ${show(err)}`) : `${CHAR_CROSS} ${show(err)}`

NotImplemented =>
IS_COLOR_ENABLED ? IO.red(`${CHAR_CROSS} not implemented`) : `${CHAR_CROSS} not implemented`
IS_COLOR_ENABLED
? IO.yellow(`${CHAR_INCOMPLETE} "${description}" not implemented`)
: `${CHAR_INCOMPLETE} "${description}" not implemented`
}


test :: String -> (String -> Wish.Wish AssertionError {}) -> Wish.Wish TestResult TestResult
export test = (description, testFunction) => pipe(
testFunction,
Expand All @@ -216,6 +234,19 @@ export test = (description, testFunction) => pipe(
),
)(description)

skip :: String -> (String -> Wish.Wish AssertionError {}) -> Wish.Wish TestResult TestResult
export skip = (description, testFunction) => pipe(
testFunction,
bimap(
pipe(
always(NotImplemented),
renderAssertionError(description),
Skipped,
),
always(Skipped(description)),
),
)(description)


generateReportSuiteEndMessage :: List (Wish.Wish e a) -> String
generateReportSuiteEndMessage = pipe(
Expand Down Expand Up @@ -410,15 +441,16 @@ runTestSuite = (suitePath, beforeAll, afterAll, testsInSuite) => pipe(
pipe(
List.reduce(mergeReports, EMPTY_REPORT),
where {
TestReport(msg, total, success, failed) =>
TestReport(msg, total, success, failed, skipped) =>
(total == 0 || failed > 0)
? TestReport(
`${suitePath}\n${msg}${generateReportSuiteEndMessage(testsInSuite)}`,
total,
success,
failed,
skipped,
)
: TestReport("", total, success, failed)
: TestReport("", total, success, failed, skipped)
},
),
),
Expand All @@ -431,6 +463,7 @@ runTestSuite = (suitePath, beforeAll, afterAll, testsInSuite) => pipe(
List.length(testsInSuite),
0,
List.length(testsInSuite),
List.length(testsInSuite),
),
)
},
Expand Down

0 comments on commit 20a2f94

Please sign in to comment.