Skip to content

Commit

Permalink
Add concept exercise test with type tests (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
SleeplessByte authored Aug 1, 2024
1 parent 62f30a3 commit 9231c48
Show file tree
Hide file tree
Showing 26 changed files with 746 additions and 15 deletions.
8 changes: 7 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@
"eslint.nodePath": ".yarn/sdks",
"prettier.prettierPath": ".yarn/sdks/prettier/index.cjs",
"typescript.tsdk": ".yarn/sdks/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true
"typescript.enablePromptUseWorkspaceTsdk": true,
"cSpell.words": [
"corepack",
"estree",
"exercism",
"tstyche"
]
}
85 changes: 71 additions & 14 deletions bin/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ if [[ "${OUTPUT}" =~ "$ROOT" ]]; then

echo "✔️ tsconfig.json from root to output"
cp "${ROOT}/tsconfig.solutions.json" "${OUTPUT}tsconfig.json"

if test -f "${OUTPUT}yarn.lock"; then
echo "✔️ renaming yarn.lock in output to prevent yarn from "
echo " interpreting this directory as a standalone package."
mv "${OUTPUT}yarn.lock" "${OUTPUT}yarn.lock.💥.bak" || true
fi;

echo ""
else
echo ""
Expand Down Expand Up @@ -256,7 +263,9 @@ else

if test -f "${OUTPUT}package.json.💥.bak"; then
echo "✔️ restoring package.json in output"
unlink "${OUTPUT}package.json"
if test -f "${OUTPUT}package.json"; then
unlink "${OUTPUT}package.json"
fi
mv "${OUTPUT}package.json.💥.bak" "${OUTPUT}package.json" || true
fi;

Expand All @@ -265,8 +274,16 @@ else
mv "${OUTPUT}tsconfig.json.💥.bak" "${OUTPUT}tsconfig.json" || true
fi;

if test -f "${OUTPUT}yarn.lock.💥.bak"; then
echo "✔️ restoring yarn.lock in output"
if test -f "${OUTPUT}yarn.lock"; then
unlink "${OUTPUT}yarn.lock"
fi
mv "${OUTPUT}yarn.lock.💥.bak" "${OUTPUT}yarn.lock" || true
fi;

result="The submitted code cannot be ran by the test-runner. There is no configuration file inside the .meta (or .exercism) directory, and the fallback test file '${test_file}' does not exist. Please fix these issues and resubmit."
echo "{ \"version\": 1, \"status\": \"error\", \"message\": \"$result\" }" > $result_file
echo "{ \"version\": 1, \"status\": \"error\", \"message\": \"${result}\" }" > $result_file
sed -Ei ':a;N;$!ba;s/\r{0,1}\n/\\n/g' $result_file

echo "❌ could not run the test suite(s). A valid output exists:"
Expand Down Expand Up @@ -366,7 +383,9 @@ if [ $test_exit -eq 2 ]; then

if test -f "${OUTPUT}package.json.💥.bak"; then
echo "✔️ restoring package.json in output"
unlink "${OUTPUT}package.json"
if test -f "${OUTPUT}package.json"; then
unlink "${OUTPUT}package.json"
fi
mv "${OUTPUT}package.json.💥.bak" "${OUTPUT}package.json" || true
fi;

Expand All @@ -376,14 +395,22 @@ if [ $test_exit -eq 2 ]; then
mv "${OUTPUT}tsconfig.json.💥.bak" "${OUTPUT}tsconfig.json" || true
fi;

if test -f "${OUTPUT}yarn.lock.💥.bak"; then
echo "✔️ restoring yarn.lock in output"
if test -f "${OUTPUT}yarn.lock"; then
unlink "${OUTPUT}yarn.lock"
fi
mv "${OUTPUT}yarn.lock.💥.bak" "${OUTPUT}yarn.lock" || true
fi;

# Compose the message to show to the student
#
# TODO: interpret the tsc_result lines and pull out the source.
# We actually already have code to do this, given the cursor position
#
tsc_result=$(cat $result_file | jq -Rsa . | sed -e 's/^"//' -e 's/"$//')
tsc_result="The submitted code didn't compile. We have collected the errors encountered during compilation. At this moment the error messages are not very read-friendly, but it's a start. We are working on a more helpful output.\n-------------------------------\n$tsc_result"
echo "{ \"version\": 1, \"status\": \"error\", \"message\": \"$tsc_result\" }" > $result_file
tsc_result="$(cat $result_file | jq -Rsa . | sed -e 's/^"//' -e 's/"$//')"
tsc_result="The submitted code didn't compile. We have collected the errors encountered during compilation. At this moment the error messages are not very read-friendly, but it's a start. We are working on a more helpful output.\n-------------------------------\n${tsc_result}"
echo "{ \"version\": 1, \"status\": \"error\", \"message\": \"${tsc_result}\" }" > $result_file
sed -Ei ':a;N;$!ba;s/\r{0,1}\n/\\n/g' $result_file

echo "❌ tsc compilation failed with a valid output:"
Expand Down Expand Up @@ -424,14 +451,14 @@ if test -d "${OUTPUT}__typetests__/"; then
echo ""
cd "${OUTPUT}" && corepack yarn tstyche --failFast 2> "${OUTPUT}tstyche.stderr.txt" 1> "${OUTPUT}tstyche.stdout.txt"

tstyche_error_output=$(cat "${OUTPUT}tstyche.stderr.txt")
tstyche_error_output="$(cat "${OUTPUT}tstyche.stderr.txt")"

if [ -z "${tstyche_error_output}" ]; then
echo "✅ all tests (*.tst.ts) passed."
else
tstyche_result=$(echo $tstyche_error_output | jq -Rsa . | sed -e 's/^"//' -e 's/"$//')
tstyche_result=$(echo "${tstyche_error_output}" | jq -Rsa . | sed -e 's/^"//' -e 's/"$//' | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g')
tstyche_result="The submitted code did compile but at least one of the type-tests failed. We have collected the failing test encountered. At this moment the error messages are not very read-friendly, but it's a start. We are working on a more helpful output.\n-------------------------------\n${tstyche_result}"
echo "{ \"version\": 1, \"status\": \"error\", \"message\": \"$tstyche_result\" }" > $result_file
echo "{ \"version\": 1, \"status\": \"error\", \"message\": \"${tstyche_result}\" }" > $result_file
sed -Ei ':a;N;$!ba;s/\r{0,1}\n/\\n/g' $result_file

echo "❌ not all tests (*.tst.ts) passed."
Expand All @@ -450,7 +477,9 @@ if test -d "${OUTPUT}__typetests__/"; then

if test -f "${OUTPUT}package.json.💥.bak"; then
echo "✔️ restoring package.json in output"
unlink "${OUTPUT}package.json"
if test -f "${OUTPUT}package.json"; then
unlink "${OUTPUT}package.json"
fi
mv "${OUTPUT}package.json.💥.bak" "${OUTPUT}package.json" || true
fi;

Expand All @@ -459,6 +488,14 @@ if test -d "${OUTPUT}__typetests__/"; then
mv "${OUTPUT}tsconfig.json.💥.bak" "${OUTPUT}tsconfig.json" || true
fi;

if test -f "${OUTPUT}yarn.lock.💥.bak"; then
echo "✔️ restoring yarn.lock in output"
if test -f "${OUTPUT}yarn.lock"; then
unlink "${OUTPUT}yarn.lock"
fi
mv "${OUTPUT}yarn.lock.💥.bak" "${OUTPUT}yarn.lock" || true
fi;

echo ""
echo "---------------------------------------------------------------"
echo "The results of this run have been written to 'results.json'."
Expand Down Expand Up @@ -496,11 +533,11 @@ if [ -z "${jest_tests}" ]; then

# TODO: use results from tstyche
runner_result="The type tests ran correctly. We are working on showing the individual tests results but for now, everything is fine!"
echo "{ \"version\": 1, \"status\": \"pass\", \"message\": \"$runner_result\" }" > $result_file
echo "{ \"version\": 1, \"status\": \"pass\", \"message\": \"${runner_result}\" }" > $result_file
else
echo "❌ neither type tests, nor execution tests ran"
runner_result="The submitted code was not subjected to any type or execution tests. It did compile correctly, but something is wrong because at least one test was expected."
echo "{ \"version\": 1, \"status\": \"error\", \"message\": \"$runner_result\" }" > $result_file
echo "{ \"version\": 1, \"status\": \"error\", \"message\": \"${runner_result}\" }" > $result_file
sed -Ei ':a;N;$!ba;s/\r{0,1}\n/\\n/g' $result_file
fi

Expand All @@ -518,7 +555,9 @@ if [ -z "${jest_tests}" ]; then

if test -f "${OUTPUT}package.json.💥.bak"; then
echo "✔️ restoring package.json in output"
unlink "${OUTPUT}package.json"
if test -f "${OUTPUT}package.json"; then
unlink "${OUTPUT}package.json"
fi
mv "${OUTPUT}package.json.💥.bak" "${OUTPUT}package.json" || true
fi;

Expand All @@ -527,6 +566,14 @@ if [ -z "${jest_tests}" ]; then
mv "${OUTPUT}tsconfig.json.💥.bak" "${OUTPUT}tsconfig.json" || true
fi;

if test -f "${OUTPUT}yarn.lock.💥.bak"; then
echo "✔️ restoring yarn.lock in output"
if test -f "${OUTPUT}yarn.lock"; then
unlink "${OUTPUT}yarn.lock"
fi
mv "${OUTPUT}yarn.lock.💥.bak" "${OUTPUT}yarn.lock" || true
fi;

echo ""
echo "---------------------------------------------------------------"
echo "The results of this run have been written to 'results.json'."
Expand Down Expand Up @@ -581,7 +628,9 @@ fi;

if test -f "${OUTPUT}package.json.💥.bak"; then
echo "✔️ restoring package.json in output"
unlink "${OUTPUT}package.json"
if test -f "${OUTPUT}package.json"; then
unlink "${OUTPUT}package.json"
fi
mv "${OUTPUT}package.json.💥.bak" "${OUTPUT}package.json" || true
fi;

Expand All @@ -590,6 +639,14 @@ if test -f "${OUTPUT}tsconfig.json.💥.bak"; then
mv "${OUTPUT}tsconfig.json.💥.bak" "${OUTPUT}tsconfig.json" || true
fi;

if test -f "${OUTPUT}yarn.lock.💥.bak"; then
echo "✔️ restoring yarn.lock in output"
if test -f "${OUTPUT}yarn.lock"; then
unlink "${OUTPUT}yarn.lock"
fi
mv "${OUTPUT}yarn.lock.💥.bak" "${OUTPUT}yarn.lock" || true
fi;

echo ""
echo "---------------------------------------------------------------"
echo "The results of this run have been written to 'results.json'."
Expand Down
16 changes: 16 additions & 0 deletions test/dev.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { join } from 'node:path'
import shelljs from 'shelljs'
import { assertPass } from './asserts.mjs'
import { fixtures } from './paths.mjs'

// run this file like:
// corepack yarn dlx cross-env SILENT=0 corepack yarn node test/dev.test.mjs

shelljs.echo(
'typescript-test-runner > passing solution (jest + tstyche) > no output directory'
)
assertPass(
'lasagna',
join(fixtures, 'lasagna', 'pass'),
join(fixtures, 'lasagna', 'pass')
)
30 changes: 30 additions & 0 deletions test/fixtures/lasagna/pass/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Hints

## 1. Define the expected oven time in minutes

- Define a [constant][constants] which should contain the [`number`][numbers] value specified in the recipe.
- [`export`][export] the constant.

## 2. Calculate the remaining oven time in minutes

- [Explicitly return a number][return] from the function.
- Use the [mathematical operator for subtraction][operators] to subtract values.

## 3. Calculate the preparation time in minutes

- [Explicitly return a number][return] from the function.
- Use the [mathematical operator for multiplication][operators] to multiply values.
- Use the extra constant for the time in minutes per layer.

## 4. Calculate the total working time in minutes

- [Explicitly return a number][return] from the function.
- [Invoke][invocation] one of the other methods implemented previously.
- Use the [mathematical operator for addition][operators] to add values.

[return]: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Return_values
[export]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export
[operators]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
[constants]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const
[invocation]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#Calling_functions
[numbers]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type
38 changes: 38 additions & 0 deletions test/fixtures/lasagna/pass/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Instructions

Lucian's girlfriend is on her way home, and he hasn't cooked their anniversary dinner!

In this exercise, you're going to write some code to help Lucian cook an exquisite lasagna from his favorite cookbook.

You have four tasks related to the time spent cooking the lasagna.

## 1. Define the expected oven time in minutes

Define the `EXPECTED_MINUTES_IN_OVEN` constant that represents how many minutes the lasagna should be in the oven. It must be exported. According to the cooking book, the expected oven time in minutes is `40`.

## 2. Calculate the remaining oven time in minutes

Implement the `remainingMinutesInOven` function that takes the actual minutes the lasagna has been in the oven as a _parameter_ and _returns_ how many minutes the lasagna still has to remain in the oven, based on the **expected oven time in minutes** from the previous task.

```javascript
remainingMinutesInOven(30)
// => 10
```

## 3. Calculate the preparation time in minutes

Implement the `preparationTimeInMinutes` function that takes the number of layers you added to the lasagna as a _parameter_ and _returns_ how many minutes you spent preparing the lasagna, assuming each layer takes you 2 minutes to prepare.

```javascript
preparationTimeInMinutes(2)
// => 4
```

## 4. Calculate the total working time in minutes

Implement the `totalTimeInMinutes` function that takes _two parameters_: the `numberOfLayers` parameter is the number of layers you added to the lasagna, and the `actualMinutesInOven` parameter is the number of minutes the lasagna has been in the oven. The function should _return_ how many minutes in total you've worked on cooking the lasagna, which is the sum of the preparation time in minutes, and the time in minutes the lasagna has spent in the oven at the moment.

```javascript
totalTimeInMinutes(3, 20)
// => 26
```
71 changes: 71 additions & 0 deletions test/fixtures/lasagna/pass/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Introduction

JavaScript is a dynamic language, supporting object-oriented, imperative, and declarative (e.g. functional programming) styles.

## (Re-)Assignment

There are a few primary ways to assign values to names in JavaScript - using variables or constants. On Exercism, variables are always written in [camelCase][wiki-camel-case]; constants are written in [SCREAMING_SNAKE_CASE][wiki-snake-case]. There is no official guide to follow, and various companies and organizations have various style guides. _Feel free to write variables any way you like_. The upside from writing them the way the exercises are prepared is that they'll be highlighted differently in the web interface and most IDEs.

Variables in JavaScript can be defined using the [`const`][mdn-const], [`let`][mdn-let] or [`var`][mdn-var] keyword.

A variable can reference different values over its lifetime when using `let` or `var`. For example, `myFirstVariable` can be defined and redefined many times using the assignment operator `=`:

```javascript
let myFirstVariable = 1
myFirstVariable = 'Some string'
myFirstVariable = new SomeComplexClass()
```

In contrast to `let` and `var`, variables that are defined with `const` can only be assigned once. This is used to define constants in JavaScript.

```javascript
const MY_FIRST_CONSTANT = 10

// Can not be re-assigned.
MY_FIRST_CONSTANT = 20
// => TypeError: Assignment to constant variable.
```

> 💡 In a later Concept Exercise the difference between _constant_ assignment / binding and _constant_ value is explored and explained.
## Function Declarations

In JavaScript, units of functionality are encapsulated in _functions_, usually grouping functions together in the same file if they belong together. These functions can take parameters (arguments), and can _return_ a value using the `return` keyword. Functions are invoked using `()` syntax.

```javascript
function add(num1, num2) {
return num1 + num2
}

add(1, 3)
// => 4
```

> 💡 In JavaScript there are _many_ different ways to declare a function. These other ways look different than using the `function` keyword. The track tries to gradually introduce them, but if you already know about them, feel free to use any of them. In most cases, using one or the other isn't better or worse.
## Exposing to Other Files

To make a `function`, a constant, or a variable available in _other files_, they need to be [exported][mdn-export] using the `export` keyword. Another file may then [import][mdn-import] these using the `import` keyword. This is also known as the module system. A great example is how all the tests work. Each exercise has at least one file, for example `lasagna.js`, which contains the _implementation_. Additionally there is at least one other file, for example `lasagna.spec.js`, that contains the _tests_. This file _imports_ the public (i.e. exported) entities in order to test the implementation:

```javascript
// file.js
export const MY_VALUE = 10

export function add(num1, num2) {
return num1 + num2
}

// file.spec.js
import { MY_VALUE, add } from './file'

add(MY_VALUE, 5)
// => 15
```

[mdn-const]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const
[mdn-export]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export
[mdn-import]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
[mdn-let]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
[mdn-var]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var
[wiki-camel-case]: https://en.wikipedia.org/wiki/Camel_case
[wiki-snake-case]: https://en.wikipedia.org/wiki/Snake_case
27 changes: 27 additions & 0 deletions test/fixtures/lasagna/pass/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"authors": ["SleeplessByte"],
"files": {
"solution": [
"lasagna.ts"
],
"test": [
"__typetests__/lasagna.tst.ts",
"lasagna.test.ts"
],
"exemplar": [
".meta/exemplar.ts"
]
},
"forked_from": [
"javascript/lasagna"
],
"blurb": "Learn the basics of TypeScript cooking a brilliant lasagna from your favorite cooking book.",
"custom": {
"version.tests.compatibility": "jest-29",
"flag.tests.task-per-describe": true,
"flag.tests.may-run-long": false,
"flag.tests.includes-optional": false,
"flag.tests.jest": true,
"flag.tests.tstyche": true
}
}
Loading

0 comments on commit 9231c48

Please sign in to comment.