2021-02-08 14:24:54 +01:00
|
|
|
# test_lib_sec_file.rb
|
2022-04-13 13:33:18 +02:00
|
|
|
require 'minitest/autorun'
|
|
|
|
require 'fileutils'
|
2021-04-27 14:08:30 +02:00
|
|
|
require_relative "../lib/lib_src_file.rb"
|
2021-02-08 14:24:54 +01:00
|
|
|
|
2022-04-13 13:33:18 +02:00
|
|
|
module Prepare_test
|
2022-04-16 08:46:29 +02:00
|
|
|
def files_srcfile
|
2022-04-13 13:33:18 +02:00
|
|
|
src_text = <<~'EOS'
|
|
|
|
This is a source file.
|
|
|
|
EOS
|
2021-04-27 14:08:30 +02:00
|
|
|
|
2022-04-13 13:33:18 +02:00
|
|
|
sample_c = <<~'EOS'
|
|
|
|
#include <stdio.h>
|
2021-02-08 14:24:54 +01:00
|
|
|
|
2022-04-13 13:33:18 +02:00
|
|
|
int
|
|
|
|
main (int argc, char **argv) {
|
|
|
|
printf ("Hello world.\n");
|
|
|
|
}
|
|
|
|
EOS
|
2021-02-08 14:24:54 +01:00
|
|
|
|
2022-04-13 13:33:18 +02:00
|
|
|
sec1_text = <<~'EOS'
|
|
|
|
This is a test file.
|
|
|
|
The sorce of `sample.c` is:
|
2021-02-08 14:24:54 +01:00
|
|
|
|
2022-04-13 13:33:18 +02:00
|
|
|
@@@include
|
|
|
|
sample.c main
|
|
|
|
@@@
|
2021-02-08 14:24:54 +01:00
|
|
|
|
2022-04-13 13:33:18 +02:00
|
|
|
It is the simplest C program.
|
|
|
|
EOS
|
2021-02-08 14:24:54 +01:00
|
|
|
|
2022-04-13 13:33:18 +02:00
|
|
|
sec2_text = <<~'EOS'
|
|
|
|
To compile the C source file `sample.c`, type:
|
2021-02-08 14:24:54 +01:00
|
|
|
|
2022-04-13 13:33:18 +02:00
|
|
|
~~~
|
|
|
|
$ gcc sample.c
|
|
|
|
~~~
|
2021-02-08 14:24:54 +01:00
|
|
|
|
2022-04-13 13:33:18 +02:00
|
|
|
Then executable file `a.out` is generated.
|
2021-02-08 14:24:54 +01:00
|
|
|
|
2022-04-13 13:33:18 +02:00
|
|
|
@@@shell
|
|
|
|
ls
|
|
|
|
@@@
|
2021-02-08 14:24:54 +01:00
|
|
|
|
2022-04-13 13:33:18 +02:00
|
|
|
To execute it, type:
|
2021-02-08 14:24:54 +01:00
|
|
|
|
2022-04-13 13:33:18 +02:00
|
|
|
~~~
|
|
|
|
$ ./a.out
|
|
|
|
~~~
|
2021-02-08 14:24:54 +01:00
|
|
|
|
2022-04-13 13:33:18 +02:00
|
|
|
The source code is in [Section 1](sec1.src.md)
|
|
|
|
EOS
|
2021-02-08 14:24:54 +01:00
|
|
|
|
2022-04-13 13:33:18 +02:00
|
|
|
sec05_text = <<~'EOS'
|
|
|
|
Prerequisite
|
2021-02-08 14:24:54 +01:00
|
|
|
|
2022-04-13 13:33:18 +02:00
|
|
|
- Linux OS like Ubuntu or Debian.
|
|
|
|
- gcc
|
|
|
|
EOS
|
2021-02-08 14:24:54 +01:00
|
|
|
|
2022-04-13 13:33:18 +02:00
|
|
|
[
|
|
|
|
["srcfile.src.md", src_text],
|
|
|
|
["sample.c", sample_c],
|
|
|
|
["sec1.src.md", sec1_text],
|
|
|
|
["sec2.src.md", sec2_text],
|
|
|
|
["sec0.5.src.md", sec05_text]
|
|
|
|
]
|
2021-04-27 14:08:30 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-13 13:33:18 +02:00
|
|
|
class Test_lib_src_file < Minitest::Test
|
|
|
|
include FileUtils
|
|
|
|
include Prepare_test
|
|
|
|
def setup
|
|
|
|
# Create sample file
|
|
|
|
@temp = get_temp_name()
|
|
|
|
Dir.mkdir @temp unless Dir.exist? @temp
|
2022-04-16 08:46:29 +02:00
|
|
|
files_srcfile().each do |f|
|
2022-04-13 13:33:18 +02:00
|
|
|
File.write "#{@temp}/#{f[0]}", f[1]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def teardown
|
|
|
|
remove_entry_secure(@temp)
|
|
|
|
end
|
|
|
|
def test_src_file
|
|
|
|
src = Src_file.new "#{@temp}/srcfile.src.md"
|
|
|
|
test_items = [
|
2022-04-16 08:46:29 +02:00
|
|
|
["path", "#{@temp}/srcfile.src.md"],
|
|
|
|
["basename", "srcfile.src.md"],
|
|
|
|
["dirname", "#{@temp}"],
|
|
|
|
["to_md", "srcfile.md"],
|
|
|
|
["to_html", "srcfile.html"],
|
|
|
|
["to_tex", "srcfile.tex"],
|
2022-04-13 13:33:18 +02:00
|
|
|
]
|
|
|
|
test_items.each do |item|
|
2022-04-16 08:46:29 +02:00
|
|
|
assert_equal item[1], src.public_send(item[0].to_sym)
|
2022-04-13 13:33:18 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
def test_sec_file
|
|
|
|
src_sec05 = Sec_file.new "#{@temp}/sec0.5.src.md"
|
|
|
|
src_sec1 = Sec_file.new "#{@temp}/sec1.src.md"
|
|
|
|
src_sec2 = Sec_file.new "#{@temp}/sec2.src.md"
|
|
|
|
test_items = [
|
2022-04-16 08:46:29 +02:00
|
|
|
["path", "#{@temp}/sec1.src.md"],
|
|
|
|
["basename", "sec1.src.md"],
|
|
|
|
["dirname", "#{@temp}"],
|
|
|
|
["c_files", [ "#{@temp}/sample.c" ]],
|
|
|
|
["to_md", "sec1.md"],
|
|
|
|
["to_html", "sec1.html"],
|
|
|
|
["to_tex", "sec1.tex"],
|
|
|
|
["num", "1"],
|
|
|
|
["to_f", 1.0],
|
|
|
|
["<=>", src_sec05, 1],
|
|
|
|
["<=>", src_sec1, 0],
|
|
|
|
["<=>", src_sec2, -1],
|
|
|
|
["is_i?", true]
|
2022-04-13 13:33:18 +02:00
|
|
|
]
|
|
|
|
test_items.each do |item|
|
2022-04-16 08:46:29 +02:00
|
|
|
if item.size == 2
|
|
|
|
assert_equal item[1], src_sec1.public_send(item[0].to_sym)
|
|
|
|
else
|
|
|
|
assert_equal item[2], src_sec1.public_send(item[0].to_sym, item[1])
|
|
|
|
end
|
2022-04-13 13:33:18 +02:00
|
|
|
end
|
|
|
|
refute src_sec05.is_i?
|
|
|
|
end
|
|
|
|
def test_sec_files
|
|
|
|
temp_renum = "temp_renum"+get_temp_name()
|
|
|
|
unless Dir.exist? temp_renum
|
|
|
|
Dir.mkdir temp_renum
|
|
|
|
end
|
2022-04-16 08:46:29 +02:00
|
|
|
files_srcfile().each do |f|
|
2022-04-13 13:33:18 +02:00
|
|
|
File.write "#{temp_renum}/#{f[0]}", f[1]
|
|
|
|
end
|
|
|
|
src_sec05 = Sec_file.new "#{temp_renum}/sec0.5.src.md"
|
|
|
|
src_sec1 = Sec_file.new "#{temp_renum}/sec1.src.md"
|
|
|
|
src_sec2 = Sec_file.new "#{temp_renum}/sec2.src.md"
|
|
|
|
sec_files = Sec_files.new [src_sec05, src_sec1, src_sec2]
|
|
|
|
sec_files.renum!
|
|
|
|
line_actual = File.read(src_sec2).match(/The source code is in \[Section \d\]\(sec\d.src.md\)\n/).to_a[0]
|
|
|
|
remove_entry_secure(temp_renum)
|
|
|
|
assert_equal ["#{temp_renum}/sec1.src.md", "#{temp_renum}/sec2.src.md", "#{temp_renum}/sec3.src.md"], sec_files
|
|
|
|
assert_equal "The source code is in [Section 2](sec2.src.md)\n", line_actual
|
|
|
|
end
|
|
|
|
private
|
|
|
|
def get_temp_name
|
|
|
|
"temp_"+Time.now.to_f.to_s.gsub(/\./,'')
|
2021-02-08 14:24:54 +01:00
|
|
|
end
|
|
|
|
end
|