3 ms·
When interacting with a system, e.g. to test it, or reproduce an error, etc. I try to build up a 'self-contained' command, rather than relying on state. For exa
by chriswarbo 4y ago
When interacting with a system, e.g. to test it, or reproduce an error, etc. I try to build up a 'self-contained' command, rather than relying on state. For example, in a REPL we can usually press UP to get a copy of the previous command, which we can then tweak (I've added // comments to explain the though process; I wouldn't actually write them down!)
// Make sure we can load our dependencies, etc.
> import foo
Imported foo.bar, foo.baz, foo.theProblematicFunction
// Great, now let's try calling the problematic code
> import foo; foo.theProblematicFunction()
Type Error: foo.theProblematicFunction requires an argument of type User
// Hmm, it needs some arguments; let's keep plugging things in until it runs
> import foo; foo.theProblematicFunction(User())
Type Error: User require arguments of type (Name, Email)
> import foo; foo.theProblematicFunction(User(Name("Alice"), Email("alice")))
Type Error: Email requires username and host parts
> import foo; foo.theProblematicFunction(User(Name("Alice"), Email("alice", "example.com")))
LOG.INFO: Sent welcome email to alice@example.com
// Great, we can run the code. Now let's try reproducing the error. Maybe it was spaces in the Name?
> import foo; foo.theProblematicFunction(User(Name("Alice Doe"), Email("alice", "example.com")))
LOG.INFO: Sent welcome email to alice@example.com
// Nope that works. Maybe it was the lack of a top-level domain?
> import foo; foo.theProblematicFunction(User(Name("Alice"), Email("alice", "localhost")))
Uncaught InvalidInputException: Email domain must contain a dot
// Aha, we've found the problem!
We now have a single line `import foo; ... "localhost")))` which reproduces what we're after. We can put that in a test suite to make a regression test; or email it to a colleague for their opinion; or file a bug in an upstream library; etc. This would be trickier we had done separate lines like `import foo`, `let name = Name("alice")`, etc.