From b8c0b2edec7d5a0cef767ef35831a2fc000e6f38 Mon Sep 17 00:00:00 2001 From: Adam Wanninger Date: Sun, 15 Oct 2017 13:36:50 -0400 Subject: [PATCH] crypto-square: tests are now generated --- exercises/crypto-square/.meta/.version | 1 + .../.meta/generator/crypto_square_case.rb | 46 +++++++ .../.meta/solutions/crypto_square.rb | 7 + exercises/crypto-square/crypto_square_test.rb | 124 ++++++++---------- 4 files changed, 112 insertions(+), 66 deletions(-) create mode 100644 exercises/crypto-square/.meta/.version create mode 100644 exercises/crypto-square/.meta/generator/crypto_square_case.rb diff --git a/exercises/crypto-square/.meta/.version b/exercises/crypto-square/.meta/.version new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/exercises/crypto-square/.meta/.version @@ -0,0 +1 @@ +1 diff --git a/exercises/crypto-square/.meta/generator/crypto_square_case.rb b/exercises/crypto-square/.meta/generator/crypto_square_case.rb new file mode 100644 index 0000000000..cb91b105d4 --- /dev/null +++ b/exercises/crypto-square/.meta/generator/crypto_square_case.rb @@ -0,0 +1,46 @@ +require 'generator/exercise_case' + +class CryptoSquareCase < Generator::ExerciseCase + + # TODO: remove guard clause when #encoded method + # is added to crypto_square.rb + def workload + return if property == 'encoded' + fix_canoncical_expected_value + indent_lines(["crypto = Crypto.new('#{plaintext}')", + "assert_equal #{expected}, #{actual}" + ], 4) + end + + private + + def actual + case property + when 'normalizedPlaintext' + 'crypto.normalize_plaintext' + when 'plaintextSegments' + 'crypto.plaintext_segments' + when 'ciphertext' + ciphertext_method_call + end + end + + def expected + canonical.expected.inspect + end + + def ciphertext_method_call + if canonical.expected.include?(' ') + 'crypto.normalize_ciphertext' + else + 'crypto.ciphertext' + end + end + + # TODO: remove when canonical data is fixed + def fix_canoncical_expected_value + return unless canonical.expected.include?(' ') + canonical.expected = canonical.expected.gsub(/\s\s/, ' ').chomp(' ') + end + +end diff --git a/exercises/crypto-square/.meta/solutions/crypto_square.rb b/exercises/crypto-square/.meta/solutions/crypto_square.rb index c0469e36f5..3ea58b370e 100644 --- a/exercises/crypto-square/.meta/solutions/crypto_square.rb +++ b/exercises/crypto-square/.meta/solutions/crypto_square.rb @@ -1,3 +1,7 @@ +module BookKeeping + VERSION = 1 +end + class Crypto def initialize(plaintext) @@ -9,6 +13,7 @@ def normalize_plaintext end def plaintext_segments + return [] if normalize_plaintext == '' normalize_plaintext.chars. each_slice(size). map{ |s| s.join('') }. @@ -20,6 +25,7 @@ def size end def ciphertext + return '' if normalize_plaintext == '' transposed.join('') end @@ -36,4 +42,5 @@ def transposed end chunks.transpose.map{ |s| s.join('') } end + end diff --git a/exercises/crypto-square/crypto_square_test.rb b/exercises/crypto-square/crypto_square_test.rb index 188ea9f129..24ccac4bd6 100644 --- a/exercises/crypto-square/crypto_square_test.rb +++ b/exercises/crypto-square/crypto_square_test.rb @@ -1,105 +1,97 @@ require 'minitest/autorun' require_relative 'crypto_square' -class CryptoTest < Minitest::Test - def test_normalize_strange_characters - crypto = Crypto.new('s#$%^&plunk') - assert_equal 'splunk', crypto.normalize_plaintext +# Common test data version: 2.0.0 bcdd704 +class CryptoSquareTest < Minitest::Test + def test_lowercase + # skip + crypto = Crypto.new('Hello') + assert_equal "hello", crypto.normalize_plaintext end - def test_normalize_uppercase_characters + def test_remove_spaces skip - crypto = Crypto.new('WHOA HEY!') - assert_equal 'whoahey', crypto.normalize_plaintext + crypto = Crypto.new('Hi there') + assert_equal "hithere", crypto.normalize_plaintext end - def test_normalize_with_numbers + def test_remove_punctuation skip - crypto = Crypto.new('1, 2, 3 GO!') - assert_equal '123go', crypto.normalize_plaintext + crypto = Crypto.new('@1, 2%, 3 Go!') + assert_equal "123go", crypto.normalize_plaintext end - def test_size_of_small_square + def test_empty_plaintext_results_in_an_empty_rectangle skip - crypto = Crypto.new('1234') - assert_equal 2, crypto.size + crypto = Crypto.new('') + assert_equal [], crypto.plaintext_segments end - def test_size_of_slightly_larger_square + def test_4_character_plaintext_results_in_an_2x2_rectangle skip - crypto = Crypto.new('123456789') - assert_equal 3, crypto.size + crypto = Crypto.new('Ab Cd') + assert_equal ["ab", "cd"], crypto.plaintext_segments end - def test_size_of_non_perfect_square + def test_9_character_plaintext_results_in_an_3x3_rectangle skip - crypto = Crypto.new('123456789abc') - assert_equal 4, crypto.size + crypto = Crypto.new('This is fun!') + assert_equal ["thi", "sis", "fun"], crypto.plaintext_segments end - def test_size_is_determined_by_normalized_plaintext + def test_54_character_plaintext_results_in_an_8x7_rectangle skip - crypto = Crypto.new('Oh hey, this is nuts!') - assert_equal 4, crypto.size + crypto = Crypto.new('If man was meant to stay on the ground, god would have given us roots.') + assert_equal ["ifmanwas", "meanttos", "tayonthe", "groundgo", "dwouldha", "vegivenu", "sroots"], crypto.plaintext_segments end - def test_plaintext_segments + def test_empty_plaintext_results_in_an_empty_encode skip - crypto = Crypto.new('Never vex thine heart with idle woes') - expected = %w(neverv exthin eheart withid lewoes) - assert_equal expected, crypto.plaintext_segments + end - def test_other_plaintext_segments + def test_non_empty_plaintext_results_in_the_combined_plaintext_segments skip - crypto = Crypto.new('ZOMG! ZOMBIES!!!') - assert_equal %w(zomg zomb ies), crypto.plaintext_segments + end - def test_ciphertext + def test_empty_plaintext_results_in_an_empty_ciphertext skip - crypto = Crypto.new('Time is an illusion. Lunchtime doubly so.') - assert_equal 'tasneyinicdsmiohooelntuillibsuuml', crypto.ciphertext + crypto = Crypto.new('') + assert_equal "", crypto.ciphertext end - def test_another_ciphertext + def test_9_character_plaintext_results_in_3_chunks_of_3_characters skip - crypto = Crypto.new('We all know interspecies romance is weird.') - assert_equal 'wneiaweoreneawssciliprerlneoidktcms', crypto.ciphertext + crypto = Crypto.new('This is fun!') + assert_equal "tsf hiu isn", crypto.normalize_ciphertext end - def test_normalized_ciphertext + def test_54_character_plaintext_results_in_7_chunks_the_last_two_padded_with_spaces skip - crypto = Crypto.new('Vampires are people too!') - assert_equal 'vrel aepe mset paoo irpo', crypto.normalize_ciphertext - end - - def test_normalized_ciphertext_spills_into_short_segment - skip - crypto = Crypto.new('Madness, and then illumination.') - expected = 'msemo aanin dnin ndla etlt shui' - assert_equal expected, crypto.normalize_ciphertext - end - - def test_another_normalized_ciphertext - skip - crypto = Crypto.new( - 'If man was meant to stay on the ground god would have given us roots', - ) - expected = 'imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau' - assert_equal expected, crypto.normalize_ciphertext - end - - def test_normalized_ciphertext_with_punctuation - skip - crypto = Crypto.new('Have a nice day. Feed the dog & chill out!') - expected = 'hifei acedl veeol eddgo aatcu nyhht' - assert_equal expected, crypto.normalize_ciphertext - end - - def test_normalized_ciphertext_when_just_less_then_a_full_square + crypto = Crypto.new('If man was meant to stay on the ground, god would have given us roots.') + assert_equal "imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau", crypto.normalize_ciphertext + end + + # Problems in exercism evolve over time, as we find better ways to ask + # questions. + # The version number refers to the version of the problem you solved, + # not your solution. + # + # Define a constant named VERSION inside of the top level BookKeeping + # module, which may be placed near the end of your file. + # + # In your file, it will look like this: + # + # module BookKeeping + # VERSION = 1 # Where the version number matches the one in the test. + # end + # + # If you are curious, read more about constants on RubyDoc: + # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html + + def test_bookkeeping skip - crypto = Crypto.new('I am') - assert_equal 'im a', crypto.normalize_ciphertext + assert_equal 1, BookKeeping::VERSION end end