Skip to content

Instantly share code, notes, and snippets.

@dead-claudia
Last active April 7, 2018 18:35
Show Gist options
  • Save dead-claudia/883e7291ca2eb04a31a4c9d6a25d7d1b to your computer and use it in GitHub Desktop.
Save dead-claudia/883e7291ca2eb04a31a4c9d6a25d7d1b to your computer and use it in GitHub Desktop.
Pipeline comparison using real-ish code (example adapted from some of my own code in the wild)
// Adapted from https://github.com/isiahmeadows/thallium/blob/b0bce2d2b7a104f5aa73b1ba0b532bcb78418d48/r/spec.js
// Original: no pipeline operators
import * as R from "../lib/reporter"
import {color as c} from "../lib/reporter"
const printIndent = (_, str) => _.print(" ".repeat(_.state.level) + str)
const getLast = (report, _, key = "name") => report.path[_.state.level - 1][key]
export default R.on("spec", {
accepts: ["write", "reset", "colors"],
create: R.consoleReporter,
before: R.setColor,
after: R.unsetColor,
init(state) {
state.level = 1
state.leaving = false
},
async report(_, report) {
if (report.isStart) {
await _.print()
} else if (report.isEnter) {
_.state.leaving = false
if (getLast(report, _, "index")) await _.print()
await printIndent(_, getLast(report, _))
_.state.level++
} else if (report.isLeave) {
_.state.level--
_.state.leaving = true
} else if (report.isPass) {
if (_.state.leaving) await _.print()
_.state.leaving = false
const speed = R.speed(report)
await printIndent(_,
c("checkmark", R.Console.symbols.Pass + " ") +
c("pass", getLast(report, _)) +
(speed === "fast" ? "" : c(speed, ` (${report.duration}ms)`))
)
} else if (report.isHook || report.isFail) {
_.pushError(report)
// Don't print the description line on cumulative hooks
if (!report.isHook || !report.isBeforeAll && !report.isAfterAll) {
if (_.state.leaving) await _.print()
_.state.leaving = false
await printIndent(_, c("fail",
`${_.errors.length}) ${getLast(report, _)}` +
R.formatRest(report)
))
}
} else if (report.isSkip) {
if (_.state.leaving) await _.print()
_.state.leaving = false
await printIndent(_, c("skip", `- ${getLast(report, _)}`))
} else if (report.isEnd) {
await _.printResults()
} else if (report.isError) {
await _.printError(report)
}
},
})
// Adapted from https://github.com/isiahmeadows/thallium/blob/b0bce2d2b7a104f5aa73b1ba0b532bcb78418d48/r/spec.js
// Variant: Elixir-style pipeline operator
import * as R from "../lib/reporter"
import {color as c} from "../lib/reporter"
const printIndent = (_, str) => _.print(" ".repeat(_.state.level) + str)
const getLast = (report, _, key = "name") => report.path[_.state.level - 1][key]
export default R.on("spec", {
accepts: ["write", "reset", "colors"],
create: R.consoleReporter,
before: R.setColor,
after: R.unsetColor,
init(state) {
state.level = 1
state.leaving = false
},
async report(_, report) {
if (report.isStart) {
await _.print()
} else if (report.isEnter) {
_.state.leaving = false
if (report |> getLast(_, "index")) await _.print()
_ |> await printIndent(report |> getLast(_))
_.state.level++
} else if (report.isLeave) {
_.state.level--
_.state.leaving = true
} else if (report.isPass) {
if (_.state.leaving) await _.print()
_.state.leaving = false
const speed = R.speed(report)
_ |> await printIndent(
c("checkmark", R.Console.symbols.Pass + " ") +
c("pass", report |> getLast(_)) +
(speed === "fast" ? "" : c(speed, ` (${report.duration}ms)`))
)
} else if (report.isHook || report.isFail) {
_.pushError(report)
// Don't print the description line on cumulative hooks
if (!report.isHook || !report.isBeforeAll && !report.isAfterAll) {
if (_.state.leaving) await _.print()
_.state.leaving = false
_ |> await printIndent(c("fail",
`${_.errors.length}) ${report |> getLast(_)}` +
R.formatRest(report)
))
}
} else if (report.isSkip) {
if (_.state.leaving) await _.print()
_.state.leaving = false
_ |> await printIndent(c("skip", `- ${report |> getLast(_)}`))
} else if (report.isEnd) {
await _.printResults()
} else if (report.isError) {
await _.printError(report)
}
},
})
// Adapted from https://github.com/isiahmeadows/thallium/blob/b0bce2d2b7a104f5aa73b1ba0b532bcb78418d48/r/spec.js
// Variant: F#-style pipeline operator
import * as R from "../lib/reporter"
import {color as c} from "../lib/reporter"
const printIndent = str => _ => _.print(" ".repeat(_.state.level) + str)
const getLast = (_, k = "name") => report => report.path[_.state.level - 1][k]
export default R.on("spec", {
accepts: ["write", "reset", "colors"],
create: R.consoleReporter,
before: R.setColor,
after: R.unsetColor,
init(state) {
state.level = 1
state.leaving = false
},
async report(_, report) {
if (report.isStart) {
await _.print()
} else if (report.isEnter) {
_.state.leaving = false
if (report |> getLast(_, "index")) await _.print()
_ |> printIndent(report |> getLast(_)) |> await
_.state.level++
} else if (report.isLeave) {
_.state.level--
_.state.leaving = true
} else if (report.isPass) {
if (_.state.leaving) await _.print()
_.state.leaving = false
const speed = R.speed(report)
_ |> printIndent(
c("checkmark", R.Console.symbols.Pass + " ") +
c("pass", report |> getLast(_)) +
(speed === "fast" ? "" : c(speed, ` (${report.duration}ms)`))
) |> await
} else if (report.isHook || report.isFail) {
_.pushError(report)
// Don't print the description line on cumulative hooks
if (!report.isHook || !report.isBeforeAll && !report.isAfterAll) {
if (_.state.leaving) await _.print()
_.state.leaving = false
_ |> printIndent(c("fail",
`${_.errors.length}) ${report |> getLast(_)}` +
R.formatRest(report)
)) |> await
}
} else if (report.isSkip) {
if (_.state.leaving) await _.print()
_.state.leaving = false
_ |> printIndent(c("skip", `- ${report |> getLast(_)}`)) |> await
} else if (report.isEnd) {
await _.printResults()
} else if (report.isError) {
await _.printError(report)
}
},
})
// Adapted from https://github.com/isiahmeadows/thallium/blob/b0bce2d2b7a104f5aa73b1ba0b532bcb78418d48/r/spec.js
// Variant: Smart pipeline operator
import * as R from "../lib/reporter"
import {color as c} from "../lib/reporter"
const printIndent = (_, str) => _.print(" ".repeat(_.state.level) + str)
const getLast = (report, _, key = "name") => report.path[_.state.level - 1][key]
export default R.on("spec", {
accepts: ["write", "reset", "colors"],
create: R.consoleReporter,
before: R.setColor,
after: R.unsetColor,
init(state) {
state.level = 1
state.leaving = false
},
async report(_, report) {
if (report.isStart) {
await _.print()
} else if (report.isEnter) {
_.state.leaving = false
if (report |> getLast(#, _, "index")) await _.print()
report
|> getLast(#, _)
|> await printIndent(_, #)
_.state.level++
} else if (report.isLeave) {
_.state.level--
_.state.leaving = true
} else if (report.isPass) {
if (_.state.leaving) await _.print()
_.state.leaving = false
const speed = R.speed(report)
_ |> await printIndent(#,
c("checkmark", R.Console.symbols.Pass + " ") +
c("pass", report |> getLast(#, _)) +
(speed === "fast" ? "" : c(speed, ` (${report.duration}ms)`))
)
} else if (report.isHook || report.isFail) {
_.pushError(report)
// Don't print the description line on cumulative hooks
if (!report.isHook || !report.isBeforeAll && !report.isAfterAll) {
if (_.state.leaving) await _.print()
_.state.leaving = false
_ |> await printIndent(#, c("fail",
`${_.errors.length}) ${report |> getLast(#, _)}` +
R.formatRest(report)
))
}
} else if (report.isSkip) {
if (_.state.leaving) await _.print()
_.state.leaving = false
report
|> getLast(#, _)
|> await printIndent(_, c("skip", `- ${#}`))
} else if (report.isEnd) {
await _.printResults()
} else if (report.isError) {
await _.printError(report)
}
},
})
// Adapted from https://github.com/isiahmeadows/thallium/blob/b0bce2d2b7a104f5aa73b1ba0b532bcb78418d48/r/spec.js
// Variant: bind operator
import * as R from "../lib/reporter"
import {color as c} from "../lib/reporter"
function printIndent(str) {
return this.print(" ".repeat(this.state.level) + str)
}
function getLast(_, key = "name") {
return this.path[_.state.level - 1][key]
}
export default R.on("spec", {
accepts: ["write", "reset", "colors"],
create: R.consoleReporter,
before: R.setColor,
after: R.unsetColor,
init(state) {
state.level = 1
state.leaving = false
},
async report(_, report) {
if (report.isStart) {
await _.print()
} else if (report.isEnter) {
_.state.leaving = false
if (report::getLast(_, "index")) await _.print()
await _::printIndent(report::getLast(_))
_.state.level++
} else if (report.isLeave) {
_.state.level--
_.state.leaving = true
} else if (report.isPass) {
if (_.state.leaving) await _.print()
_.state.leaving = false
const speed = R.speed(report)
await _::printIndent(
c("checkmark", R.Console.symbols.Pass + " ") +
c("pass", report::getLast(_)) +
(speed === "fast" ? "" : c(speed, ` (${report.duration}ms)`))
)
} else if (report.isHook || report.isFail) {
_.pushError(report)
// Don't print the description line on cumulative hooks
if (!report.isHook || !report.isBeforeAll && !report.isAfterAll) {
if (_.state.leaving) await _.print()
_.state.leaving = false
await _::printIndent(c("fail",
`${_.errors.length}) ${report::getLast(_)}` +
R.formatRest(report)
))
}
} else if (report.isSkip) {
if (_.state.leaving) await _.print()
_.state.leaving = false
await _::printIndent(c("skip", `- ${report::getLast(_)}`))
} else if (report.isEnd) {
await _.printResults()
} else if (report.isError) {
await _.printError(report)
}
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment