Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Stdlib, Spec-lib] Myst spec failures now includes path to the failure #161

Merged
merged 5 commits into from
Feb 16, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/myst/syntax/lexer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,11 @@ module Myst
when "\\n" then '\n'
when "\\\"" then '"'
when "\\t" then '\t'
when "\\e" then '\e'
when "\\r" then '\r'
when "\\f" then '\f'
when "\\v" then '\v'
when "\\b" then '\b'
when "\\0" then '\0'
end
end
Expand Down
39 changes: 39 additions & 0 deletions stdlib/colored.mt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
defmodule Color
def ansi_from_symbol(sym)
when sym == :black
return ANSI_BLACK
when sym == :red
return ANSI_RED
when sym == :green
return ANSI_GREEN
when sym == :yellow
return ANSI_YELLOW
when sym == :blue
return ANSI_BLUE
when sym == :purple
return ANSI_PURPLE
when sym == :cyan
return ANSI_CYAN
when sym == :white
return ANSI_WHITE
else
raise ":\"<(sym)>\" is not a valid color"
end
end

ANSI_RESET = "\e[0m"

ANSI_BLACK = "\e[0;30m"
ANSI_RED = "\e[0;31m"
ANSI_GREEN = "\e[0;32m"
ANSI_YELLOW = "\e[0;33m"
ANSI_BLUE = "\e[0;34m"
ANSI_PURPLE = "\e[0;35m"
ANSI_CYAN = "\e[0;36m"
ANSI_WHITE = "\e[0;37m"

def colored(string, sym)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having a type restriction on sym would make sense here. Since the only valid input is a symbol, it doesn't make sense to accept anything else.

color = ansi_from_symbol(sym)
"<(color)><(string.to_s)><(ANSI_RESET)>"
end
end
9 changes: 9 additions & 0 deletions stdlib/spec.mt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require "./spec/dsl.mt"
require "./spec/errors.mt"
require "./spec/single_spec.mt"
require "./spec/describe_container.mt"
require "./colored.mt"

include Spec

# Spec
#
Expand All @@ -24,6 +28,9 @@ require "./spec/single_spec.mt"
# Calls). This should be addressed before too long, since it's a fairly common
# use case, but a basic Spec library does not require it.
defmodule Spec
describe_stack = []
include DSL

def it(name, &block)
spec = %SingleSpec{name}
spec.run{ block() }
Expand All @@ -39,6 +46,8 @@ defmodule Spec


def describe(name, &block)
describe_stack.push(%DescribeContainer{name})
block()
describe_stack.pop
end
end
17 changes: 17 additions & 0 deletions stdlib/spec/describe_container.mt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Spec
deftype DescribeContainer
def initialize(name : String)
@name = name
end

def name; @name; end

def get_path(current : String)
when !describe_stack.empty?
return describe_stack.pop.get_path("<(@name)> <(current)>")
Copy link
Member

@faultyserver faultyserver Feb 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't actually run this yet, but is pop the desired behavior here? Seems like it would be modifying the stack when you're just trying to get a value from it.

Looks like we don't have a List#last method that could replace it anyway. Could be worth adding (along with List#first)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, i just though it didn't matter as the program exits when a spec fails. I should do a first and last still though, for #140.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually that wouldn't work here anyways if there was nested describes. So i'll figure something else

else
"<(@name)> <(current)>"
end
end
end
end
7 changes: 5 additions & 2 deletions stdlib/spec/single_spec.mt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ defmodule Spec

def run(&block)
block()
STDOUT.print(".")
STDOUT.print(Color.colored(".", :green))
rescue failure
STDOUT.puts(failure)
STDOUT.puts("\n")
STDOUT.puts(Color.colored(" <(describe_stack.pop.get_path(@name))>", :red))
STDOUT.puts(Color.colored(" <(failure)>", :red))
STDOUT.puts
exit(1)
end
end
Expand Down