Sha256: 2f79c14d6a77d8efdfc24a56e573d1d23039194bc28f3a1ed406344a5f9d2e69

Contents?: true

Size: 1.74 KB

Versions: 26

Compression:

Stored size: 1.74 KB

Contents

source("./word-count.R")
library(testthat)

# When comparing lists, all.equal expects the objects to be in the same order
# This expectation instead checks that a) the set of names are the same and
# b) each named object is equal
expect_equal_pairs <- function(object, expected) {
  expect_equal(sort(names(object)),
               sort(names(expected)),
               info = "names in lists differ")
  for (name in names(expected)) {
    expect_equal(object[name], expected[name], info = "list element missing")
  }
}

test_that("count one word", {
  expect_equal_pairs(word_count("word"),
                     list("word" = 1))
})

test_that("count one of each word", {
  expect_equal_pairs(word_count("one of each"),
                     list(
                       "one" = 1,
                       "of" = 1,
                       "each" = 1
                     ))
})

test_that("multiple occurrences of a word", {
  expect_equal_pairs(
    word_count("one fish two fish red fish blue fish"),
    list(
      "one" = 1,
      "fish" = 4,
      "two" = 1,
      "red" = 1,
      "blue" = 1
    )
  )
})

test_that("ignore punctuation", {
  expect_equal_pairs(
    word_count("car : carpet as java : javascript!!&@$%^&"),
    list(
      "car" = 1,
      "carpet" = 1,
      "as" = 1,
      "java" = 1,
      "javascript" = 1
    )
  )
})

test_that("include numbers", {
  expect_equal_pairs(word_count("testing, 1, 2 testing"),
                     list(
                       "testing" = 2,
                       "1" = 1,
                       "2" = 1
                     ))
})

test_that("normalize case", {
  expect_equal_pairs(word_count("go Go GO Stop stop"),
                     list("go" = 3, "stop" = 2))
})

print("All tests passed for exercise: word-count")

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
trackler-2.1.0.27 tracks/r/exercises/word-count/test_word-count.R
trackler-2.1.0.26 tracks/r/exercises/word-count/test_word-count.R
trackler-2.1.0.25 tracks/r/exercises/word-count/test_word-count.R
trackler-2.1.0.24 tracks/r/exercises/word-count/test_word-count.R
trackler-2.1.0.23 tracks/r/exercises/word-count/test_word-count.R
trackler-2.1.0.22 tracks/r/exercises/word-count/test_word-count.R