Fork me on GitHub
#beginners
<
2018-05-14
>
lepistane03:05:54

Hi, how does one deal with unsupported escape chars in strings? "[1A],[1ZA],[3B],[8A],[19A]|http:\/\/www.site.rs\/place\/img1.jpg|I" this is response i get from one site which is location of a picture. i just get erros what ever i do saying \/ is unsupported which is ok but i can't even replace them or do anything with that kind of string. How do i deal with this?

seancorfield04:05:49

@lepistane How are you trying to read the string? You should be able to read it as raw bytes, then replace \/ then process it as strings.

lum14:05:42

In a luminus project, for a postgres connection, in dev-config.edn, should it be with or without jdbc in :database-url "jdbc:?

lepistane19:05:20

well test it 😄 try both see how it goes once you finish you will know it's without jdbc

lum14:05:15

the template just showed

joelv14:05:45

So I'm trying write some unit tests where I need to mock some imported static java functions below

(ns app.foo
  (:import [com.auth0.jwt JWT JWTVerifier]))

(defn decode-token [JWT token]
  (try
    (JWT/decode token)
    (catch Exception e
      (log/warn "Invalid Token: " e)
      e)))

;;;TEST FILE

(ns app.foo-test
  (:import [com.auth0.jwt JWT JWTVerifier]))

(def mock-jwt (proxy [JWT] []
                (decode [^String a] "decoded")))

(deftest decode-token-test
  (testing "should decode"
    (println (decode-token mock-jwt "foo"))))

joelv14:05:21

mock-jwt/decode is not being overloaded by my proxy call. What am I doing wrong?

dpsutton15:05:51

it's a little confusing that you have imported JWT and also have a parameter called JWT. I'm honestly not sure which is resolved there. I'm also not convinced this test tests any actual code besides the try/catch and logging but that doesn't seem to be what you are testing. But I would do something like this:

joelv15:05:29

@dpsutton I'm aware of that but we are trying to be strict about code coverage here.

joelv15:05:35

also thought it would be better to pass in JWT to the function so that I can easily mock it and the function becomes pure.

noisesmith15:05:10

the problem here is having an object with the same name as the class

noisesmith15:05:17

(or at least potential problem)

noisesmith15:05:49

if your param is x x/foo shouldn't work - you can't call static methods on classes passed as args like that

noisesmith15:05:08

if x/foo is referring to the class x which was imported, your param is being ignored

mikerod16:05:50

@ If interested in more details on that subject: Clojure has no secret power when it comes to doing something like a static method call on a class (this is just Java interop), it information needed at compile-time, otherwise you have to use reflection.

mikerod16:05:30

If you are familiar with Java, think of how you would try to do what you what you did there in Java and you’d run into the same problem

mikerod16:05:36

void blah ( Class<JWT> jwtClass ) {

jwtClass.decode("thing"); // not going to work
}

mind16:05:14

You might find with-redefs useful. It lets you redefine a function for testing without having to pass it in as an argument.

noisesmith16:05:18

replacing a parameter in order to use with-redefs is a bad idea - with-redefs is fragile, even for testing

mind16:05:25

How is it fragile?

noisesmith16:05:42

it's a global mutation that has no respect for scope

noisesmith16:05:14

and it only works on some functions - anything statically compiled or special cased in the clojure compiler won't be redeffed properly

noisesmith16:05:56

in order to replace the class with something you with-redef, you need it to be a var, at that point you've already done the fix that allows you to use it as an arg

mind16:05:14

It only redefines things within the with-redefs block though. We use it frequently for testing and it helps us write code that doesn’t care about the details of how it’s being tested.

noisesmith16:05:27

it doesn't, it applies to all threads until that block exits

noisesmith16:05:36

it means you can't run any tests concurrently

mind16:05:19

Hm, interesting to know 🙂 That’s not something we’re doing so I never came across that.

dpsutton16:05:46

i'm a big fan of the higher order type and make it easy to pass in a test param. and then def or defn a version in the code that correctly composes the actual implementation. I've found that writing code that cares about the details of how its being tested makes cleaner code often

noisesmith16:05:59

thanks @alexmiller I was looking for a good writeup and failing :D

veix.q522:05:52

how do I programmatically access the source code of a function as a string?

veix.q522:05:41

source functionName -> String

veix.q522:05:52

nevermind I found it in clojure.repl/source-fn