Skip to content

Commit

Permalink
Add tests for IniFile
Browse files Browse the repository at this point in the history
One of the main functionality that is still untested is the
'tildeexpand'.
  • Loading branch information
havardAasen committed Jan 8, 2024
1 parent 242d9ff commit c973a13
Show file tree
Hide file tree
Showing 28 changed files with 161 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/inifile/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Tests for the library `liblinuxcncini`.

This is the library created from the files `src/libnml/inifile/inifile.*`. To
simplify the testing we utilize the application `inivar`.
4 changes: 4 additions & 0 deletions tests/inifile/comments/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Test the INI parser with regards to comments, both inline and on it's own line.

After 2.7.x, the INI parser in LinuxCNC does not support inline comments.
See: https://github.com/LinuxCNC/linuxcnc/issues/991
4 changes: 4 additions & 0 deletions tests/inifile/comments/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
value1
value2 ; inline comment
value3
value4 # another inline comment
6 changes: 6 additions & 0 deletions tests/inifile/comments/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

inivar -ini with_comments.ini -var key1
inivar -ini with_comments.ini -var key2
inivar -ini with_comments.ini -var key3
inivar -ini with_comments.ini -var key4
8 changes: 8 additions & 0 deletions tests/inifile/comments/with_comments.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
; This is a comment
[section1]
key1=value1
; Another comment
key2=value2 ; inline comment
# different comment style
key3=value3
key4=value4 # another inline comment
7 changes: 7 additions & 0 deletions tests/inifile/continuation_lines/continuation_lines.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[section1]
key1=value1
key2=value2 \
value3 \
value4 \
value5
key3=value6
1 change: 1 addition & 0 deletions tests/inifile/continuation_lines/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
value2 value3 value4 value5
3 changes: 3 additions & 0 deletions tests/inifile/continuation_lines/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

inivar -ini continuation_lines.ini -var key2
7 changes: 7 additions & 0 deletions tests/inifile/duplicated_keys/duplicated_keys.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[section1]
key1=value1
key1=value2

[section2]
key1=value3
key1=value4
4 changes: 4 additions & 0 deletions tests/inifile/duplicated_keys/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
value1
value1
value4
value3
6 changes: 6 additions & 0 deletions tests/inifile/duplicated_keys/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

inivar -ini duplicated_keys.ini -var key1
inivar -ini duplicated_keys.ini -var key1 -sec section1 -num 1
inivar -ini duplicated_keys.ini -var key1 -sec section2 -num 2
inivar -ini duplicated_keys.ini -var key1 -num 3
5 changes: 5 additions & 0 deletions tests/inifile/line_endings/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The INI parser in LinuxCNC handles UNIX line endings, '\n' and DOS '\r\n', with
a warning. The parser aborts when it encounters classic MAC OS line endings '\r'.

The INI files is created in the test script, since several tools automatically
fixes the line endings.
2 changes: 2 additions & 0 deletions tests/inifile/line_endings/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
value1
value1
16 changes: 16 additions & 0 deletions tests/inifile/line_endings/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

# Manually create INI files with different line endings.
lf="[section1]\nkey1=value1\n"
cr="[section1]\rkey1=value1\r"
crlf="[section1]\r\nkey1=value1\r\n"

echo -e "$lf" > "lf.ini"
echo -e "$cr" > "cr.ini"
echo -e "$crlf" > "crlf.ini"

inivar -ini lf.ini -var key1 # Should be successful.
inivar -ini cr.ini -var key1 # Should fail.
inivar -ini crlf.ini -var key1 # Should be successful.

rm -f lf.ini cr.ini crlf.ini
1 change: 1 addition & 0 deletions tests/inifile/missing_values/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
value1
1 change: 1 addition & 0 deletions tests/inifile/missing_values/missing_section.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
key1=value1
2 changes: 2 additions & 0 deletions tests/inifile/missing_values/missing_value.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[section1]
key1=
5 changes: 5 additions & 0 deletions tests/inifile/missing_values/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

inivar -ini missing_section.ini -sec section1 -var key1
inivar -ini missing_value.ini -var key1
inivar -ini missing_section.ini -var key1
6 changes: 6 additions & 0 deletions tests/inifile/python_bindings/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
value1
value2
value3
Did not find missing value
a long value
0.000001
17 changes: 17 additions & 0 deletions tests/inifile/python_bindings/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/python3

import linuxcnc

inifile = linuxcnc.ini("values.ini")

missing_value = inifile.find("section1", "missing")
value_with_space = inifile.find("section1", "key3")
numbered_value = inifile.find("section1", "number")
list_of_values = inifile.findall("section2", "key1")

for value in list_of_values:
print(value)
if missing_value is None:
print("Did not find missing value")
print(value_with_space)
print(numbered_value)
12 changes: 12 additions & 0 deletions tests/inifile/python_bindings/values.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[section1]
key1=value1
key2=value 2
key3 = a long value
a long key = value4
number = 0.000001

[section2]
key1=value1
key1=value2
key1=value3
key4=value4
5 changes: 5 additions & 0 deletions tests/inifile/values/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
value1
value 2
a long value
value4
0.000001
7 changes: 7 additions & 0 deletions tests/inifile/values/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

inivar -ini values.ini -var key1
inivar -ini values.ini -var key2
inivar -ini values.ini -var key3
inivar -ini values.ini -var "a long key"
inivar -ini values.ini -var number
6 changes: 6 additions & 0 deletions tests/inifile/values/values.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[section1]
key1=value1
key2=value 2
key3 = a long value
a long key = value4
number = 0.000001
5 changes: 5 additions & 0 deletions tests/inifile/whitespace/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
value1
value1
value1
value1
value2
2 changes: 2 additions & 0 deletions tests/inifile/whitespace/leading_whitespace.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[section1]
key1=value1
2 changes: 2 additions & 0 deletions tests/inifile/whitespace/space_between_equal.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[section1]
key1 = value1
13 changes: 13 additions & 0 deletions tests/inifile/whitespace/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# Manually create INI file with trailing whitespace.
ini_content="[section1] \nkey1=value1\t\nkey2=value2 \n"
echo -e "$ini_content" > "trailing_whitespace.ini"

inivar -ini leading_whitespace.ini -var key1
inivar -ini leading_whitespace.ini -sec section1 -var key1
inivar -ini space_between_equal.ini -var key1
inivar -ini trailing_whitespace.ini -var key1
inivar -ini trailing_whitespace.ini -var key2

rm -f trailing_whitespace.ini

0 comments on commit c973a13

Please sign in to comment.