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

WIP: Test page's code blocks #74

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
46 changes: 46 additions & 0 deletions spec/code_block_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'spec_helper'
require 'kramdown'
require 'ap'
require 'pp'
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove debug requirements.

site = File.join(File.dirname(__FILE__), '..','content', '**', '*.md')
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not site, but rather content, right?

PAGES = Dir.glob(site)

def extract_code(element,prev_element)
codes = []
if element.type == :codespan \
&& !element.value.nil? \
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this ever occur? It could probable be removed and .value converted to empty string to return failure as the :codespan is empty.

BTW. I quite prefer codespan instead of codeblock.

&& !codes.include?(element.value) \
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, I'd prefer to include even the already existing codes and only avoid executing / checking the same one multiple times.
That's because it preserves the context information we may need in other tests(related with the comment above).

&& ((prev_element.children.size == 1) || (element.value[/^\n/])) #element value hasn't got any sibling or begin with '\n'
Copy link
Contributor

Choose a reason for hiding this comment

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

Please move the comment above the if.

codes.push element.value
end
element.children.each do |child|
codes += extract_code(child, element)
end
codes
end

PAGES.each do |page|
generated_page = Kramdown::Document.new(File.read(page))
codes = extract_code(generated_page.root,generated_page)
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like you could pass just generated_page instead and use .root where needed.


describe 'Code blocks on markdown' do


before :each do
codes.each do |code|
code.gsub! "\n",''
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd rather this would be solved in the extractor itself. (multiple lines = multiple commands)

end
end
it 'have good syntax' do
codes.each do |code|
if code[/^\$/] #code starts with '$'
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems to me like we want to have different tests for various code-lines (commands).
Meaning this logic could be deciding for the test enablement, but from the level above.

expect(system('bash', '-n', '-c', code, '&>/dev/null')).to be_truthy,"Something wrong with: '#{code}'"
Copy link
Contributor

Choose a reason for hiding this comment

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

We could build an Array of code-lines + return-codes (per page) and run the tests afterwards.

elsif code[/^\#/] #code starts with '#'
Copy link
Contributor

Choose a reason for hiding this comment

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

You're actually comparing only the first character from string, which can be done as:

code[0] == ?#

#ap code
else #code starts with something else
#ap code
end
end
end
end
end