Skip to content

Commit

Permalink
Improve testability
Browse files Browse the repository at this point in the history
  • Loading branch information
hbiede committed May 31, 2022
1 parent 7fa74a9 commit 5285e61
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
11 changes: 4 additions & 7 deletions gen_tokens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,20 @@ def self.write_latex_to_pdf(org, org_tex)
end
end

# Create a unique PDF for a singular organization with its passwords and
# moves it to the 'pdfs' directory
# Create a the content for a single organization's password PDF
#
# @param [String] tex_file The contents of the Latex template
# @param [String] org The name of the organization
# @param [Array<String>] org_passwords A collection of passwords for a given
# organization
def self.create_org_pdf(tex_file, org, org_passwords)
def self.create_latex_content(tex_file, org, org_passwords)
org_tex = tex_file.clone

org_tex = org_tex.gsub('REPLACESCHOOL', org)

# Has to be triple escaped to account for the un-escaping done by ruby, then regex, then latex
password_text = org_passwords.join(" \\\\\\\\\n")
org_tex = org_tex.gsub('REPLACEPW', password_text)

write_latex_to_pdf(org, org_tex)
org_tex.gsub('REPLACEPW', password_text)
end

# Print a progress report for the token report generation
Expand All @@ -87,7 +84,7 @@ def self.print_progress_report(index, org, number_of_orgs, longest_name_length)
def self.create_pdfs(all_tokens, tex_file)
longest_org_name = all_tokens.keys.max_by(&:length).length
all_tokens.each_with_index do |(org, org_passwords), i|
create_org_pdf(tex_file, org, org_passwords)
write_latex_to_pdf(org, create_latex_content(tex_file, org, org_passwords))
printf(print_progress_report(i, org, all_tokens.size, longest_org_name))
end
# Clear the progress bar
Expand Down
36 changes: 20 additions & 16 deletions tests/gen_tokens_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,26 @@ def test_print_progress_report
assert_equal("\r8.33%% [== ]: PDF generated for Test ", PDFWriter.print_progress_report(0, 'Test', 12, 15))
end

def test_create_org_pdf
begin
assert_true(PDFWriter.create_org_pdf("\\documentclass{article}\n\\begin{document}\nREPLACESCHOOL\n\\end{document}", "John Adams", ["Password 1", "Password 2"]))
assert_latex_equal('JohnAdams', "\\documentclass{article}\n\\begin{document}\nJohn Adams\n\\end{document}")

assert_true(PDFWriter.create_org_pdf("\\documentclass{article}\n\\begin{document}\nREPLACESCHOOL, REPLACESCHOOL\n\\end{document}", "John Adams", ["Password 1", "Password 2"]))
assert_latex_equal('JohnAdams', "\\documentclass{article}\n\\begin{document}\nJohn Adams, John Adams\n\\end{document}")
ensure
File.delete "pdfs/JohnAdams.pdf"
end

assert_true(PDFWriter.create_org_pdf("\\documentclass{article}\n\\begin{document}\nREPLACEPW\n\\end{document}", "Thomas Jefferson", ["Password 1", "Password 2"]))
assert_latex_equal('ThomasJefferson', "\\documentclass{article}\n\\begin{document}\nPassword 1 \\\\\nPassword 2\n\\end{document}")

assert_true(PDFWriter.create_org_pdf("\\documentclass{article}\n\\begin{document}\nREPLACEPW, REPLACEPW\n\\end{document}", "Thomas Jefferson 2", ["Password 1", "Password 2"]))
assert_latex_equal('ThomasJefferson2', "\\documentclass{article}\n\\begin{document}\nPassword 1 \\\\\nPassword 2, Password 1 \\\\\nPassword 2\n\\end{document}")
def test_create_latex_content
assert_equal(
PDFWriter.create_latex_content("\\documentclass{article}\n\\begin{document}\nREPLACESCHOOL\n\\end{document}", "John Adams", ["Password 1", "Password 2"]),
"\\documentclass{article}\n\\begin{document}\nJohn Adams\n\\end{document}"
)

assert_equal(
PDFWriter.create_latex_content("\\documentclass{article}\n\\begin{document}\nREPLACESCHOOL, REPLACESCHOOL\n\\end{document}", "John Adams", ["Password 1", "Password 2"]),
"\\documentclass{article}\n\\begin{document}\nJohn Adams, John Adams\n\\end{document}"
)

assert_equal(
PDFWriter.create_latex_content("\\documentclass{article}\n\\begin{document}\nREPLACEPW\n\\end{document}", "Thomas Jefferson", ["Password 1", "Password 2"]),
"\\documentclass{article}\n\\begin{document}\nPassword 1 \\\\\nPassword 2\n\\end{document}"
)

assert_equal(
PDFWriter.create_latex_content("\\documentclass{article}\n\\begin{document}\nREPLACEPW, REPLACEPW\n\\end{document}", "Thomas Jefferson 2", ["Password 1", "Password 2"]),
"\\documentclass{article}\n\\begin{document}\nPassword 1 \\\\\nPassword 2, Password 1 \\\\\nPassword 2\n\\end{document}"
)
end

def test_create_pdfs
Expand Down

0 comments on commit 5285e61

Please sign in to comment.