Bind more, more tests

This commit is contained in:
Roberto Alsina 2023-07-14 16:23:55 -03:00
parent d55a1c6bad
commit 44619dd1d4
2 changed files with 50 additions and 39 deletions

View File

@ -2,44 +2,21 @@ require "./spec_helper"
include Shortcodes
describe "Shortcodes" do
# TODO: Write tests
it "works" do
input = "foo{{% bar %}}baz{{% /bar %}}qux"
result = parse(input)
result.shortcodes.size.should eq 1
result.shortcodes[0].args.size.should eq 0
result.shortcodes[0].name.should eq "bar"
it "should parse empty string" do
result = parse("")
result.shortcodes.size.should eq 0
result.errors.size.should eq 0
end
end
# Port tests below to spec
# #include <cgreen/cgreen.h>
# #include <bglibs/str.h>
# #include "shortcodes.h"
# Describe(parse);
# BeforeEach(parse) {}
# AfterEach(parse) {}
# sc_result result;
# str s;
# void chunk_s(char *buffer, chunk c)
# {
# str_copyb(&s, buffer + c.start, c.len);
# }
# Ensure(parse, empty_string)
# {
# char *input = "";
# result = parse(input);
# // This means no shortcodes in it
# assert_that(result.sccount, is_equal_to(0));
# assert_that(result.errcount, is_equal_to(0));
# }
it "should parse simeple shortcode" do
result = parse("foobar {{% shortcode %}}blah")
result.shortcodes.size.should eq 1
result.errors.size.should eq 0
result.shortcodes[0].name.should eq "shortcode"
result.shortcodes[0].matching.should eq 0
result.shortcodes[0].args.size.should eq 0
result.shortcodes[0].whole.should eq "{{% shortcode %}}"
end
# Ensure(parse, simple_shortcode)
# {
# char *input = "foobar {{% shortcode %}}blah";
@ -241,3 +218,4 @@ end
# // // No shortcodes
# // assert_that(result.sc[0].name.len, is_equal_to(0));
# // }
end

View File

@ -30,7 +30,7 @@ lib LibShortcodes
fun parse(input : Pointer(LibC::Char), len : UInt32) : ScResult
end
module Shortcodes
module Shortcodes
struct Arg
property name : String = ""
property value : String = ""
@ -39,18 +39,28 @@ module Shortcodes
end
end
struct Error
@position: UInt32
@code: UInt32
def initialize(@position, @code)
end
end
struct Shortcode
property name : String = ""
property data : String = ""
property matching : Int32 = 0
property args : Array(Arg) = [] of Arg
def initialize(@name, @data, @matching, @args)
property errors : Array(Error) = [] of Error
property whole : String = ""
def initialize(@name, @data, @matching, @args, @whole)
end
end
struct Result
property shortcodes : Array(Shortcode) = [] of Shortcode
property errors : Array(Error) = [] of Error
end
def extract(c : LibShortcodes::Chunk, s : String)
@ -70,13 +80,36 @@ module Shortcodes
extract(sc.argvals[j], input),
)
end
errors = [] of Error
(0...r.errcount).each do |k|
errors << Error.new(
r.errors[k].position,
r.errors[k].code,
)
end
result.shortcodes << Shortcode.new(
extract(sc.name, input),
extract(sc.data, input),
sc.matching,
args,
extract(sc.whole, input),
)
end
result
end
#### Error codes
#
# You are closing the wrong shortcode.
# Example:
# {{% foo %}} {{% /bar %}}
ERR_MISMATCHED_CLOSING_TAG = 1
# You are using mismatched brackets.
# Example:
# {{% foo >}}
ERR_MISMATCHED_BRACKET = 2
end