0

I've currently got a function in my main file which squares every number in the list, now that works perfectly with no errors but my unit tests keeps on throwing errors and I cant seem to figure out why.

 (ns squareone.core-test
 (:require [clojure.test :refer :all]
        [squareone.core :refer :all]))

(deftest Squaring
  (testing "Squaring test"
    (is (= [1 16] (square [1 4])))))
  That is everything in my test file 

And this is what is in my main file minus the actual squaring function

(ns squareone.core)
(:require '[clojure.string :as string])
(require '['clojure.string :as 'str])

And I get this error

Syntax error (ClassCastException) compiling at 
(C:\Users\public\squareone\src\squareone\core.clj:3:1).
class clojure.lang.PersistentList cannot be cast to class clojure.lang.Symbol 
(clojure.lang.PersistentList and clojure.lang.Symbol are in unnamed module of loader 'app')

Very unsure on how to proceed, any help is appreciated :)

1
  • Please check syntax for ns and for require. Parens and quotes. Commented Jan 17, 2021 at 23:24

1 Answer 1

4

The syntax error is on line 3 which is:

(require '['clojure.string :as 'str])

and we can confirm in a REPL that is indeed illegal:

user=> (require '['clojure.string :as 'str])
Execution error (ClassCastException) at user/eval1 (REPL:1).
class clojure.lang.PersistentList cannot be cast to class clojure.lang.Named (clojure.lang.PersistentList and clojure.lang.Named are in unnamed module of loader 'app')

You also have a problem on line 3, but it doesn't cause a syntax or runtime error:

(:require '[clojure.string :as string])

This attempts to look up the keyword :require in the literal vector [clojure.string :as string] -- the leading quote (') prevents evaluation so that is a symbol (clojure.string) followed by a keyword (:as) followed by another symbol (string). Looking up a keyword :require in a vector is going to return nil.

I expect what you were trying to do was this:

(ns squareone.core
  (:require [clojure.string :as string]))

Note where the parentheses are: the :require form is part of the ns declaration.

At this point, you should be able to delete line 3 and move on to testing.

Why is line 3 illegal though?

The quote (') is shorthand for wrapping a form in (quote ..) and that in turn prevents evaluation. You have three quotes in line 3, so it expands to this:

(require (quote [(quote clojure.string) :as (quote str)]))

The outer quote prevents evaluation, so the argument that is passed to the require function is: [(quote clojure.string) :as (quote str)] which is a vector containing a list with two symbols (quote and clojure.string), a keyword (:as), and another list of two symbols. But require expects just a symbol in those slots like this:

(require '[clojure.string :as str])

and that's the error you are getting: expected clojure.lang.Symbol but got clojure.lang.PersistentList.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.