Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJeremiah Shore
31,168 Pointsbug in test of Compose Price Conversion Functions code challenge?
Originally, I thought there may be a bug in the test code of this code challenge, but after some digging, it looks like a character encoding issue or something else strange like that.
After my changes as part of the code challenge, all tests pass but one: argentinaRecordsAreFormattedAndConverted
. This gets a little weird and redonks, so bear with me...
The assertion error output I'm getting from the test is:
java.lang.AssertionError:
Expected: iterable containing ["First is $1.00 (USD) which is $15,48 (Argentine Peso)", "Second is $2.00 (USD) which is $30,96 (Argentine Peso)"]
but: item 0: was "First is $1.00 (USD) which is $ 15,48 (Argentine Peso)"
I played around some and added some console output to this test to confirm the string representation of the local statements
variable. You can see from this, the string output is already different; there is some extra spacing right after the $ signs.
System.out.println(statements);
//[First is $1.00 (USD) which is $ 15,48 (Argentine Peso), Second is $2.00 (USD) which is $ 30,96 (Argentine Peso)]
Next I checked this ouput against multiple calls to contains()
, which passed. Note that I COPIED and PASTED the output from the console to create this step.
boolean condition =
statements.contains("First is $1.00 (USD) which is $ 15,48 (Argentine Peso)")
&& statements.contains("Second is $2.00 (USD) which is $ 30,96 (Argentine Peso)");
assert(condition);
Next I condensed the above expression and assertion into a single statement, like the original test. Note that for this code I COPIED, PASTED, and then EDITED the code from above, and did so right below it. Another critical detail to notice is that this test is identical to the original test, with the only exception being that there is a space after the $ characters in the expected values.
assertThat(statements, contains(
"First is $1.00 (USD) which is $ 15,48 (Argentine Peso)",
"Second is $2.00 (USD) which is $ 30,96 (Argentine Peso)"
));
Up to this point, the new assertions I introduced are passing, so it looks like this may be due to some locale or formatting issue. However, I then edited the original test code in place to add a space after the $ characters. I re-ran the tests and this assertion failed, even though the code is identical to the code aboved it!!
//original, modified test
assertThat(statements, contains(
"First is $1.00 (USD) which is $ 15,48 (Argentine Peso)",
"Second is $2.00 (USD) which is $ 30,96 (Argentine Peso)"
));
I then tried comparing literals to see what would happen, and this is where it got strange...
assert("nothing is real" == "nothing is real"); //passes
assert("nothing is real".equals("nothing is real")); //passes, just showing you can do either with literals
assert("First is $1.00 (USD) which is $ 15,48 (Argentine Peso)" == "First is $1.00 (USD) which is $ 15,48 (Argentine Peso)"); //both copied from output, passes
assert("First is $1.00 (USD) which is $ 15,48 (Argentine Peso)" == "First is $1.00 (USD) which is $ 15,48 (Argentine Peso)"); //both copied from original
//fails, 1st literal copied from output, second literal is the modified original code
assert("First is $1.00 (USD) which is $ 15,48 (Argentine Peso)" == "First is $1.00 (USD) which is $ 15,48 (Argentine Peso)");
//also fails, just showing use of .equals
assert("First is $1.00 (USD) which is $ 15,48 (Argentine Peso)".equals("First is $1.00 (USD) which is $ 15,48 (Argentine Peso)"));
At this point, I ran both copied/pasted snippets through an online diff tool, and you can see that specific result here. Notice the removal/addition on the number characters? I think that's the culprit. Problem is... I don't know what to do with this information.
Is this a character encoding problem? Is this because I'm working on Win10 and (I'm assuming) this project was built on a Mac? What can I do to get this test to pass this code challenge specification? What about the extra spacing after the $; what's that about?
I believe the correct answer is as I've demonstrated in this diff (spoilers!); would also like to know if I got that right. ;)