Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ format:
generate-tests:
@echo "Generating tests for all exercises..."
@for exercise in $(EXERCISES); do \
if [ -f exercises/practice/$$exercise/.meta/generateTests.js ]; then \
if [ -f exercises/practice/$$exercise/.meta/testTemplate.js ]; then \
echo "-> Generating: $$exercise"; \
node exercises/practice/$$exercise/.meta/generateTests.js || exit 1; \
node exercises/practice/$$exercise/.meta/testTemplate.js || exit 1; \
else \
echo "-> Skipping: $$exercise (no generator found)"; \
fi \
Expand Down
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "two-fer",
"name": "Two-Fer",
"uuid": "2a03dbf7-5aae-4859-8fde-b51c11cab02f",
"practices": [],
"prerequisites": [],
"difficulty": 2
}
]
},
Expand Down
11 changes: 0 additions & 11 deletions exercises/practice/hello-world/.meta/generateTests.js

This file was deleted.

21 changes: 15 additions & 6 deletions exercises/practice/hello-world/.meta/testTemplate.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
export const slug = 'hello-world';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { stringEqual } from "../../../../test_generator/assertions.js";
import { generateTests } from '../../../../test_generator/testGenerator.js';

export const assertionFunctions = `let stringEqual = (~message=?, a: string, b: string) =>
assertion(~message?, ~operator="stringEqual", (a, b) => a == b, a, b)`;
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const slug = path.basename(path.resolve(__dirname, '..'))

export const template = (c, moduleName) => {
return `stringEqual(~message="${c.description}", ${moduleName}.hello(), "${c.expected}")`
}
// EDIT THIS WITH YOUR ASSERTIONS
export const assertionFunctions = [ stringEqual ]

// EDIT THIS WITH YOUR TEST TEMPLATES
export const template = (c) => {
return `stringEqual(~message="${c.description}", hello(), "${c.expected}")`
}

generateTests(__dirname, slug, assertionFunctions, template)
9 changes: 5 additions & 4 deletions exercises/practice/hello-world/tests/HelloWorld_test.res
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
open Test
open HelloWorld

let stringEqual = (~message=?, a: string, b: string) =>
assertion(~message?, ~operator="stringEqual", (a, b) => a == b, a, b)
let stringEqual = (~message=?, a: string, b: string) => assertion(~message?, ~operator="stringEqual", (a, b) => a == b, a, b)

test("Say Hi!", () => {
stringEqual(~message="Say Hi!", HelloWorld.hello(), "Hello, World!")
})
stringEqual(~message="Say Hi!", hello(), "Hello, World!")
})

24 changes: 24 additions & 0 deletions exercises/practice/two-fer/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Instructions

Your task is to determine what you will say as you give away the extra cookie.

If you know the person's name (e.g. if they're named Do-yun), then you will say:

```text
One for Do-yun, one for me.
```

If you don't know the person's name, you will say _you_ instead.

```text
One for you, one for me.
```

Here are some examples:

| Name | Dialogue |
| :----- | :-------------------------- |
| Alice | One for Alice, one for me. |
| Bohdan | One for Bohdan, one for me. |
| | One for you, one for me. |
| Zaphod | One for Zaphod, one for me. |
8 changes: 8 additions & 0 deletions exercises/practice/two-fer/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Introduction

In some English accents, when you say "two for" quickly, it sounds like "two fer".
Two-for-one is a way of saying that if you buy one, you also get one for free.
So the phrase "two-fer" often implies a two-for-one offer.

Imagine a bakery that has a holiday offer where you can buy two cookies for the price of one ("two-fer one!").
You take the offer and (very generously) decide to give the extra cookie to someone else in the queue.
6 changes: 6 additions & 0 deletions exercises/practice/two-fer/.meta/TwoFer.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let twoFer = name => {
switch name {
| None => "One for you, one for me."
| Some(nameString) => "One for " ++ nameString ++ ", one for me."
}
}
22 changes: 22 additions & 0 deletions exercises/practice/two-fer/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"authors": [
"tejasbubane",
"therealowenrees"
],
"files": {
"solution": [
"src/TwoFer.res"
],
"test": [
"tests/TwoFer_test.res"
],
"example": [
".meta/TwoFer.res"
],
"editor": [
"src/TwoFer.resi"
]
},
"blurb": "Create a sentence of the form \"One for X, one for me.\".",
"source_url": "https://github.com/exercism/problem-specifications/issues/757"
}
21 changes: 21 additions & 0 deletions exercises/practice/two-fer/.meta/testTemplate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { stringEqual } from "../../../../test_generator/assertions.js";
import { generateTests } from '../../../../test_generator/testGenerator.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const slug = path.basename(path.resolve(__dirname, '..'))

// EDIT THIS WITH YOUR ASSERTIONS
export const assertionFunctions = [ stringEqual ]

// EDIT THIS WITH YOUR TEST TEMPLATES
export const template = (c) => {
if (c.input.name) {
return `stringEqual(~message="${c.description}", twoFer(Some("${c.input.name}")), "${c.expected}")`
} else {
return `stringEqual(~message="${c.description}", twoFer(None), "${c.expected}")`
}
}

generateTests(__dirname, slug, assertionFunctions, template)
19 changes: 19 additions & 0 deletions exercises/practice/two-fer/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[1cf3e15a-a3d7-4a87-aeb3-ba1b43bc8dce]
description = "no name given"

[b4c6dbb8-b4fb-42c2-bafd-10785abe7709]
description = "a name given"

[3549048d-1a6e-4653-9a79-b0bda163e8d5]
description = "another name given"
15 changes: 15 additions & 0 deletions exercises/practice/two-fer/rescript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@exercism/rescript",
"sources": [
{ "dir": "src", "subdirs": true, "type": "dev" },
{ "dir": "tests", "subdirs": true, "type": "dev" }
],
"package-specs": [
{
"module": "esmodule",
"in-source": true
}
],
"suffix": ".res.js",
"dev-dependencies": ["rescript-test"]
}
1 change: 1 addition & 0 deletions exercises/practice/two-fer/src/TwoFer.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let twoFer = _ => panic("'twoFer not yet implemented")
1 change: 1 addition & 0 deletions exercises/practice/two-fer/src/TwoFer.resi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let twoFer: option<string> => string
17 changes: 17 additions & 0 deletions exercises/practice/two-fer/tests/TwoFer_test.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
open Test
open TwoFer

let stringEqual = (~message=?, a: string, b: string) => assertion(~message?, ~operator="stringEqual", (a, b) => a == b, a, b)

test("no name given", () => {
stringEqual(~message="no name given", twoFer(None), "One for you, one for me.")
})

test("a name given", () => {
stringEqual(~message="a name given", twoFer(Some("Alice")), "One for Alice, one for me.")
})

test("another name given", () => {
stringEqual(~message="another name given", twoFer(Some("Bob")), "One for Bob, one for me.")
})

1 change: 1 addition & 0 deletions problem-specifications
Submodule problem-specifications added at 0ebef3
1 change: 1 addition & 0 deletions test_generator/assertions.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 17 additions & 7 deletions test_generator/testGenerator.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import fs from 'node:fs';
import path from 'node:path';
import { template } from '../exercises/practice/hello-world/.meta/testTemplate.js';
import getValidCases from './getCases.js';

export const toPascalCase = (slug) =>
export const generateTests = (dir, slug, assertionFunctions, template) => {
const outputPath = path.resolve(dir, '..', 'tests', `${toPascalCase(slug)}_test.res`);
const cases = getValidCases(slug);
generate(outputPath, slug, cases, assertionFunctions, template);
}

const toPascalCase = (slug) =>
slug.split('-').map(w => w.charAt(0).toUpperCase() + w.slice(1)).join('');

export const generate = (outputPath, slug, cases, config) => {
const generate = (outputPath, slug, cases, assertionFunctions, template) => {
const moduleName = toPascalCase(slug);

let output = `open Test\n\n`;
output += `${config.assertionFunctions}\n\n`;
let output = `open Test
open ${moduleName}\n\n`;

if (Array.isArray(assertionFunctions)) {
output += assertionFunctions.map(fn => fn.trim()).join('\n\n') + '\n\n';
}

cases.forEach((c) => {
output += `test("${c.description}", () => {
${template(c, moduleName)}
})`
${template(c)}
})\n\n`
});

if (!fs.existsSync(path.dirname(outputPath))) {
Expand Down