🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of The-F00L

The-F00L's solution

to Bob in the CFML Track

Published at Apr 14 2021 · 0 comments
Instructions
Test suite
Solution

Bob is a lackadaisical teenager. In conversation, his responses are very limited.

Bob answers 'Sure.' if you ask him a question.

He answers 'Whoa, chill out!' if you yell at him.

He says 'Fine. Be that way!' if you address him without actually saying anything.

He answers 'Whatever.' to anything else.


To run the code in this exercise, you will only need to have CommandBox CLI installed. This binary runs CFML code from the command line.

To run the tests, cd into the exercise folder and run the following:

box task run TestRunner
# Or start up a test watcher that will rerun when files change
box task run TestRunner --:watcher

The tests leverage a library called TestBox which supports xUnit and BDD style of testing. All test suites will be written in the BDD style which uses closures to define test specs. You won't need to worry about installing TestBox. The CLI test runner will take care of that for you. You just need to be connected to the internet the first time you run it. You can read more about it here:

https://testbox.ortusbooks.com/content/

Source

Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=06

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

BobTest.cfc

component extends="testbox.system.BaseSpec" {

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

	function run(){
	
		describe( "My Bob class", function(){			

			it( 'stating something', function(){
				expect( SUT.response( heyBob='Tom-ay-to,  tom-aaaah-to.' ) ).toBe( 'Whatever.' );
			});

			it( 'shouting', function(){
				expect( SUT.response( heyBob='WATCH OUT!' ) ).toBe( 'Whoa, chill out!' );
			});

			it( 'shouting gibberish', function(){
				expect( SUT.response( heyBob='FCECDFCAAB' ) ).toBe( 'Whoa, chill out!' );
			});

			it( 'asking a question', function(){
				expect( SUT.response( heyBob='Does this cryogenic chamber make me look fat?' ) ).toBe( 'Sure.' );
			});

			it( 'asking a numeric question', function(){
				expect( SUT.response( heyBob='You are,  what,  like 15?' ) ).toBe( 'Sure.' );
			});

			it( 'asking gibberish', function(){
				expect( SUT.response( heyBob='fffbbcbeab?' ) ).toBe( 'Sure.' );
			});

			it( 'talking forcefully', function(){
				expect( SUT.response( heyBob='Let''s go make out behind the gym!' ) ).toBe( 'Whatever.' );
			});

			it( 'using acronyms in regular speech', function(){
				expect( SUT.response( heyBob='It''s OK if you don''t want to go to the DMV.' ) ).toBe( 'Whatever.' );
			});

			it( 'forceful question', function(){
				expect( SUT.response( heyBob='WHAT THE HELL WERE YOU THINKING?' ) ).toBe( 'Calm down, I know what I''m doing!' );
			});

			it( 'shouting numbers', function(){
				expect( SUT.response( heyBob='1,  2,  3 GO!' ) ).toBe( 'Whoa, chill out!' );
			});

			it( 'only numbers', function(){
				expect( SUT.response( heyBob='1,  2,  3' ) ).toBe( 'Whatever.' );
			});

			it( 'question with only numbers', function(){
				expect( SUT.response( heyBob='4?' ) ).toBe( 'Sure.' );
			});

			it( 'shouting with special characters', function(){
				expect( SUT.response( heyBob='ZOMG THE %^*@##$(*^ ZOMBIES ARE COMING!!11!!1!' ) ).toBe( 'Whoa, chill out!' );
			});

			it( 'shouting with no exclamation mark', function(){
				expect( SUT.response( heyBob='I HATE YOU' ) ).toBe( 'Whoa, chill out!' );
			});

			it( 'statement containing question mark', function(){
				expect( SUT.response( heyBob='Ending with ? means a question.' ) ).toBe( 'Whatever.' );
			});

			it( 'non-letters with question', function(){
				expect( SUT.response( heyBob=':) ?' ) ).toBe( 'Sure.' );
			});

			it( 'prattling on', function(){
				expect( SUT.response( heyBob='Wait! Hang on. Are you going to be OK?' ) ).toBe( 'Sure.' );
			});

			it( 'silence', function(){
				expect( SUT.response( heyBob='' ) ).toBe( 'Fine. Be that way!' );
			});

			it( 'prolonged silence', function(){
				expect( SUT.response( heyBob='          ' ) ).toBe( 'Fine. Be that way!' );
			});

			it( 'alternate silence', function(){
				expect( SUT.response( heyBob='										' ) ).toBe( 'Fine. Be that way!' );
			});

			it( 'multiple line question', function(){
				expect( SUT.response( heyBob='#chr( 10 )#Does this cryogenic chamber make me look fat?#chr( 10 )#no' ) ).toBe( 'Whatever.' );
			});

			it( 'starting with whitespace', function(){
				expect( SUT.response( heyBob='         hmmmmmmm...' ) ).toBe( 'Whatever.' );
			});

			it( 'ending with whitespace', function(){
				expect( SUT.response( heyBob='Okay if like my  spacebar  quite a bit?   ' ) ).toBe( 'Sure.' );
			});

			it( 'other whitespace', function(){
				expect( SUT.response( heyBob='#chr( 10 )##chr( 13 )# 	' ) ).toBe( 'Fine. Be that way!' );
			});

			it( 'non-question ending with whitespace', function(){
				expect( SUT.response( heyBob='This is a statement ending with whitespace      ' ) ).toBe( 'Whatever.' );
			});

		});
		
	}
 
}

SolutionTest.cfc

component extends="BobTest" {

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

}
/**
* Your implmentation of the Bob exercise
*/
component {

	/**
	* @returns
	*/
	//talking forcefully failed
	function response( heyBob ) {
		tempLL=arrayLen(heyBob.split(' '));
		regLL=len(reFind("[A-Z\^s]",heyBob));
		if ((isEmpty(heyBob))||(len(reReplace(heyBob,"[\t\s\r]+",''))==0)){
			return "Fine. Be that way!";
		}else if ((right(replace(heyBob," ","","ALL"),1)=="?")&&(tempLL<=len(rereplace(heyBob,"[A-Z?]",'',"ALL")))){
			return "Sure.";
		}else if ((right(replace(heyBob," ","","ALL"),1)=="?")&&(tempLL==len(rereplace(heyBob,"[A-Z]",'',"ALL")))){
			return "Calm down, I know what I'm doing!";
		}else if ((right(replace(heyBob," ","","ALL"),1)=="!")||(tempLL>=len(rereplace(heyBob,"[A-Z!]",'',"ALL")))){
			return "Whoa, chill out!";
		}else{
			return "Whatever.";
		}

	}

}

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?