Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add two-fer exercise #196

Merged
merged 3 commits into from
Nov 15, 2023
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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,14 @@
"difficulty": 5,
"topics": null
},
{
"slug": "two-fer",
"name": "Two Fer",
"uuid": "7a64fd35-c6f0-4c0c-90c8-f72bcca77a93",
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "run-length-encoding",
"name": "Run Length Encoding",
Expand Down
25 changes: 25 additions & 0 deletions exercises/practice/two-fer/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Instructions

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

If your friend likes cookies, and is named Do-yun, then you will say:

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

If your friend doesn't like cookies, you give the cookie to the next person in line at the bakery.
Since you don't know their 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 go for the offer and (very generously) decide to give the extra cookie to a friend.
13 changes: 13 additions & 0 deletions exercises/practice/two-fer/.meta/Example.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Your implementation of the TwoFer exercise
*/
component {

/**
* @returns
*/
function twoFer( name="you" ) {
return "One for " & name & ", one for me."
}

}
7 changes: 7 additions & 0 deletions exercises/practice/two-fer/.meta/ExampleTest.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
component extends="TwoFerTest" {

function beforeAll(){
SUT = createObject( 'Solution' );
}

}
19 changes: 19 additions & 0 deletions exercises/practice/two-fer/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"TwoFer.cfc"
],
"test": [
"TwoFerTest.cfc"
],
"example": [
".meta/Example.cfc",
".meta/ExampleTest.cfc"
]
},
"blurb": "Create a sentence of the form \"One for X, one for me.\".",
"source_url": "https://github.com/exercism/problem-specifications/issues/757"
}
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"
103 changes: 103 additions & 0 deletions exercises/practice/two-fer/TestRunner.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* I am a CommandBox task runner which you can use to test your implementation of this exercise against the
* provided test suite. To use me, open the CommandBox CLI and run this:
*
* CommandBox> task run TestRunner
*
* To start up a test watcher that will automatically rerun the test suite every time you save a file change, run this:
*
* CommandBox> task run TestRunner --watcher
*
*/
component {

/**
* @solution Runs the tests against the solution
* @watcher Start up a file watch that re-runs the tests on file changes. Use Ctrl-C to stop
*/
function run( boolean solution=false, boolean watcher=false ) {

ensureTestBox();

if( watcher ) {

// Tabula rasa
command( 'cls' ).run();

// Start watcher
watch()
.paths( '*.cfc' )
.inDirectory( getCWD() )
.withDelay( 500 )
.onChange( function() {

// Clear the screen
command( 'cls' )
.run();

// This is neccessary so changes to tests get picked up right away.
pagePoolClear();

runTests( solution );

} )
.start();

} else {
runTests( solution );
}

}

/**
* Make sure the TestBox framework is installed
*/
private function ensureTestBox() {
var excerciseRoot = getCWD();
var testBoxRoot = excerciseRoot & '/testbox';

if( !directoryExists( testBoxRoot ) ) {

print.boldYellowLine( 'Installing some missing dependencies for you!' ).toConsole();
command( 'install' )
.inWorkingDirectory( excerciseRoot )
.run();
}

// Bootstrap TestBox framework
filesystemUtil.createMapping( '/testbox', testBoxRoot );
}

/**
* Invoke TestBox to run the test suite
*/
private function runTests( boolean solution=false ) {

// Create TestBox and run the tests
testData = new testbox.system.TestBox()
.runRaw( directory = {
// Find all CFCs...
mapping = filesystemUtil.makePathRelative( getCWD() ),
// ... in this directory ...
recurse = false,
// ... whose name ends in "test"
filter = function( path ) {
return path.reFind( ( solution ? 'Solution' : '' ) & 'Test.cfc$' );
}
} )
.getMemento();

// Print out the results with ANSI formatting for the CLI
getInstance( 'CLIRenderer@testbox-commands' )
.render( print, testData, true );

print.toConsole();

// Set proper exit code
if( testData.totalFail || testData.totalError ) {
setExitCode( 1 );
}
}

}

13 changes: 13 additions & 0 deletions exercises/practice/two-fer/TwoFer.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Your implementation of the TwoFer exercise
*/
component {

/**
* @returns
*/
function twoFer( name ) {
// Implement me here
}

}
27 changes: 27 additions & 0 deletions exercises/practice/two-fer/TwoFerTest.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
component extends="testbox.system.BaseSpec" {

function beforeAll(){
SUT = createObject( 'TwoFer' );
}

function run(){

describe( "My TwoFer class", function(){

it( 'no name given', function(){
expect( SUT.twoFer() ).toBe( 'One for you, one for me.' );
});

it( 'a name given', function(){
expect( SUT.twoFer( name='Alice' ) ).toBe( 'One for Alice, one for me.' );
});

it( 'another name given', function(){
expect( SUT.twoFer( name='Bob' ) ).toBe( 'One for Bob, one for me.' );
});

});

}

}
8 changes: 8 additions & 0 deletions exercises/practice/two-fer/box.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"dependencies":{
"testbox":"^2.5.0+107"
},
"installPaths":{
"testbox":"testbox/"
}
}
37 changes: 37 additions & 0 deletions exercises/practice/two-fer/index.cfm
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!---

This file will only be used if you want to start up a web server in this directory. You can do so by running:

$> box
CommandBox> install
CommandBox> server start

However, this is not necessary unless you really just want to use the HTML reporters on TestBox.
Ideally, you'll skip the need for this file entirely and just run the tests directly frm the CLI like this:

CommandBox> task run TestRunner

--->
<cfsetting showDebugOutput="false">
<cfparam name="url.reporter" default="simple">
<cfscript>
// get a list of all CFCs in this folder whose name looks like "XXXTest.cfc"
// And turn it into compnent path relative to the web root
url.bundles = directoryList(
path=expandPath( '/' ),
filter='*Test.cfc' )
.map( function( path ) {
return path
.replaceNoCase( expandPath( '/' ), '' )
.left( -4 )
} )
.toList();
</cfscript>

<!--- Ensure TestBox --->
<cfif fileExists( "/testbox/system/runners/HTMLRunner.cfm" )>
<!--- Include the TestBox HTML Runner --->
<cfinclude template="/testbox/system/runners/HTMLRunner.cfm">
<cfelse>
Oops, you don't have TestBox installed yet! Please run <b>box install</b> from the root of your excercise folder and refresh this page.
</cfif>