Skip to content

Commit

Permalink
add format check on ci (#75)
Browse files Browse the repository at this point in the history
* add format check on ci

* format

* isntall shfmtr

* format
  • Loading branch information
yuchiki authored Nov 30, 2024
1 parent 8e9e203 commit 17b37f7
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 143 deletions.
16 changes: 14 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,22 @@
"customizations": {
"vscode": {
"extensions": [
"task.vscode-task"
]
"task.vscode-task",
"ms-dotnettools.csharp",
"ms-dotnettools.csdevkit",
"mkhl.shfmt"
],
"settings": {
"[csharp]": {
"editor.defaultFormatter": "ms-dotnettools.csharp"
},
"[shellscript]": {
"editor.defaultFormatter": "mkhl.shfmt"
}
}
}
},
"onCreateCommand": "curl -sS https://webi.sh/shfmt | sh",

"postCreateCommand": "dotnet restore"
}
12 changes: 12 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ jobs:
dotnet-version: 7.0.x
- name: build
run: dotnet build
check-if-formatted:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Task
uses: arduino/setup-task@v1
with:
version: 3.x
- name: install shfmt
run: task install-shfmt
- name: Check if formatted
run: task check-if-formatted
unit-test:
runs-on: ubuntu-latest
permissions:
Expand Down
15 changes: 15 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,24 @@ tasks:
cmds:
- shisoku.Integration/integration_calc.sh
silent: true
format:
desc: "Formats the code"
cmds:
- shfmt -w .
- dotnet format
check-if-formatted:
desc: "Checks if the code is formatted"
cmds:
- shfmt -d .
- dotnet format --verify-no-changes
install-shfmt:
desc: "Installs shfmt"
cmds:
- curl -sS https://webi.sh/shfmt | sh
check-all:
desc: "Runs all checks"
cmds:
- task check-if-formatted
- task build
- task unit-test
- task integration-test
176 changes: 88 additions & 88 deletions shisoku.Integration/integration_calc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,102 +4,102 @@ BINARY_DIR="bin"
BINARY="${BINARY_DIR}/shisoku"

function main() {
dotnet publish shisoku --configuration Release --output ${BINARY_DIR}
dotnet publish shisoku --configuration Release --output ${BINARY_DIR}

local sum_exit_code=0
run_test_exp_case "足し算" "1+1;" 2
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "掛け算" "1*1;" 1
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "()付きの計算式" "(1+1)*3;" 6
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "割算が優先" "1+6/3;" 3
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "()の後に()が来ている" "(1+1)(3)" error
sum_exit_code=$((sum_exit_code + $?))
# なぜ上が正しくないかというとパースした後に全てのTokenが使い切れる構文木が生成されないから。
# Tokenが余っていたら弾く
# TODO 本体コードを修正する
run_test_exp_case "空白は全て無視するよって正しい計算式が空白で区切られても問題ない" "1+1 +2;" 4
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "(が終了していない" "(1+1" error
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "文字列を入れると失敗する" "aaaa" error
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "演算記号のみを入れると失敗する" "---" error
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "正解が負の数になっても失敗しない" "1-2;" -1
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "変数の定義をしても問題ない" "const a:int =1;" "Unit"
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "変数の計算をしても問題ない" "const a: int=1;a+1;" error
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "複数の計算を出せる" "1-2;1;" error
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "Booleanが扱える" "1==1;" True
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "Booleanが扱える(False)" "2==1;" False
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "Booleanが扱える(bool同士)" "true==true;" True
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "Booleanが混ざった複合演算ができる" "true== (1+1 == 2);" True
sum_exit_code=$((sum_exit_code + $?))
run_test_file_case "複数分の入ったファイルを読み込んで実行できる" "shisoku.Integration/test.shisoku" "2"
sum_exit_code=$((sum_exit_code + $?))
run_test_file_case "名前なし関数の実行ができる" "shisoku.Integration/namelessFunctionTest.shisoku" "8"
sum_exit_code=$((sum_exit_code + $?))
run_test_file_case "関数内で関数を定義できる" "shisoku.Integration/functionCanReturnFunction.shisoku" "12"
sum_exit_code=$((sum_exit_code + $?))
run_test_file_case "関数ないで変数を定義できる" "shisoku.Integration/functionCanHaveConstStatement.shisoku" "14"
sum_exit_code=$((sum_exit_code + $?))
run_test_file_case "再帰関数を定義できる" "shisoku.Integration/RecursionFunction.shisoku" "5050"
sum_exit_code=$((sum_exit_code + $?))
run_test_file_case "ユークリッドの互除法" "shisoku.Integration/EuclideanAlgorithm.shisoku" "61"
sum_exit_code=$((sum_exit_code + $?))
local sum_exit_code=0
run_test_exp_case "足し算" "1+1;" 2
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "掛け算" "1*1;" 1
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "()付きの計算式" "(1+1)*3;" 6
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "割算が優先" "1+6/3;" 3
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "()の後に()が来ている" "(1+1)(3)" error
sum_exit_code=$((sum_exit_code + $?))
# なぜ上が正しくないかというとパースした後に全てのTokenが使い切れる構文木が生成されないから。
# Tokenが余っていたら弾く
# TODO 本体コードを修正する
run_test_exp_case "空白は全て無視するよって正しい計算式が空白で区切られても問題ない" "1+1 +2;" 4
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "(が終了していない" "(1+1" error
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "文字列を入れると失敗する" "aaaa" error
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "演算記号のみを入れると失敗する" "---" error
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "正解が負の数になっても失敗しない" "1-2;" -1
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "変数の定義をしても問題ない" "const a:int =1;" "Unit"
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "変数の計算をしても問題ない" "const a: int=1;a+1;" error
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "複数の計算を出せる" "1-2;1;" error
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "Booleanが扱える" "1==1;" True
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "Booleanが扱える(False)" "2==1;" False
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "Booleanが扱える(bool同士)" "true==true;" True
sum_exit_code=$((sum_exit_code + $?))
run_test_exp_case "Booleanが混ざった複合演算ができる" "true== (1+1 == 2);" True
sum_exit_code=$((sum_exit_code + $?))
run_test_file_case "複数分の入ったファイルを読み込んで実行できる" "shisoku.Integration/test.shisoku" "2"
sum_exit_code=$((sum_exit_code + $?))
run_test_file_case "名前なし関数の実行ができる" "shisoku.Integration/namelessFunctionTest.shisoku" "8"
sum_exit_code=$((sum_exit_code + $?))
run_test_file_case "関数内で関数を定義できる" "shisoku.Integration/functionCanReturnFunction.shisoku" "12"
sum_exit_code=$((sum_exit_code + $?))
run_test_file_case "関数ないで変数を定義できる" "shisoku.Integration/functionCanHaveConstStatement.shisoku" "14"
sum_exit_code=$((sum_exit_code + $?))
run_test_file_case "再帰関数を定義できる" "shisoku.Integration/RecursionFunction.shisoku" "5050"
sum_exit_code=$((sum_exit_code + $?))
run_test_file_case "ユークリッドの互除法" "shisoku.Integration/EuclideanAlgorithm.shisoku" "61"
sum_exit_code=$((sum_exit_code + $?))

if [ ${sum_exit_code} == "0" ]; then
return 0
else
return 1
fi
if [ ${sum_exit_code} == "0" ]; then
return 0
else
return 1
fi
}

function run_test_exp_case() {
local case_name="$1"
local input="$2"
local expected="$3"
local case_name="$1"
local input="$2"
local expected="$3"

local output
output=$($BINARY --exp "${input}" 2>/dev/null)
local error_code=$?
if [ "${output}" = "${expected}" ]; then
echo "${case_name}:Pass"
return 0
elif [ "${expected}" = "error" ] && [ "${error_code}" -ne '0' ]; then
echo "${case_name}:Pass"
return 0
else
echo "${case_name}:Fail output is ${output}. Shoud be ${expected}"
return 1
fi
local output
output=$($BINARY --exp "${input}" 2>/dev/null)
local error_code=$?
if [ "${output}" = "${expected}" ]; then
echo "${case_name}:Pass"
return 0
elif [ "${expected}" = "error" ] && [ "${error_code}" -ne '0' ]; then
echo "${case_name}:Pass"
return 0
else
echo "${case_name}:Fail output is ${output}. Shoud be ${expected}"
return 1
fi
}
function run_test_file_case() {
local case_name="$1"
local input="$2"
local expected="$3"
local case_name="$1"
local input="$2"
local expected="$3"

local output
output=$($BINARY --file "${input}" 2>/dev/null)
local error_code=$?
if [ "${output}" = "${expected}" ]; then
echo "${case_name}:Pass"
return 0
elif [ "${expected}" = "error" ] && [ "${error_code}" -ne '0' ]; then
echo "${case_name}:Pass"
return 0
else
echo "${case_name}:Fail output is ${output}. Shoud be ${expected}"
return 1
fi
local output
output=$($BINARY --file "${input}" 2>/dev/null)
local error_code=$?
if [ "${output}" = "${expected}" ]; then
echo "${case_name}:Pass"
return 0
elif [ "${expected}" = "error" ] && [ "${error_code}" -ne '0' ]; then
echo "${case_name}:Pass"
return 0
else
echo "${case_name}:Fail output is ${output}. Shoud be ${expected}"
return 1
fi
}
main
26 changes: 13 additions & 13 deletions shisoku.Tests/CalcTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public void NumberEvaluates()
public void AddEvaluates()
{
var expectedValue = new IntValue(12);
var value = shisoku.CalcExpression.Calc(new AddExpression(new NumberExpression(5), new NumberExpression(7) ,new Checked(new IntType())), new VariableEnvironment());
var value = shisoku.CalcExpression.Calc(new AddExpression(new NumberExpression(5), new NumberExpression(7), new Checked(new IntType())), new VariableEnvironment());
Assert.Equal<Value>(expectedValue, value);
}
[Fact]
public void SubEvaluates()
{
var expectedValue = new IntValue(12);
var value = shisoku.CalcExpression.Calc(new SubExpression(new NumberExpression(16), new NumberExpression(4),new Checked(new IntType())), new VariableEnvironment());
var value = shisoku.CalcExpression.Calc(new SubExpression(new NumberExpression(16), new NumberExpression(4), new Checked(new IntType())), new VariableEnvironment());
Assert.Equal<Value>(expectedValue, value);
}
[Fact]
Expand All @@ -44,7 +44,7 @@ public void DivEvaluates()
public void ModEvaluates()
{
var expectedValue = new IntValue(1);
var value = shisoku.CalcExpression.Calc(new ModExpression(new NumberExpression(25), new NumberExpression(2),new Checked(new IntType())), new VariableEnvironment());
var value = shisoku.CalcExpression.Calc(new ModExpression(new NumberExpression(25), new NumberExpression(2), new Checked(new IntType())), new VariableEnvironment());
Assert.Equal<Value>(expectedValue, value);
}
[Fact]
Expand Down Expand Up @@ -92,7 +92,7 @@ public void EqualEvaluatesToFalseWhenTrueAndFalseAreGiven()
[Fact]
public void EqualExpressionDoesNotEvaluateWhenDiffrentTypeArgmentsAreGiven()
{
Assert.Throws<Exception>(() => shisoku.CalcExpression.Calc(new EqualExpression(new BoolExpression(true), new NumberExpression(12) ), new VariableEnvironment()));
Assert.Throws<Exception>(() => shisoku.CalcExpression.Calc(new EqualExpression(new BoolExpression(true), new NumberExpression(12)), new VariableEnvironment()));
}
[Fact]
public void AddExpressionDoesNotEvaluateWhenBooleanTypeArgmentsAreGiven()
Expand All @@ -107,18 +107,18 @@ public void SubExpressionDoesNotEvaluateWhenBooleanTypeArgmentsAreGiven()
[Fact]
public void MulExpressionDoesNotEvaluateWhenBooleanTypeArgmentsAreGiven()
{
Assert.Throws<Exception>(() => shisoku.CalcExpression.Calc(new MulExpression(new BoolExpression(true), new BoolExpression(true),new Checked(new BoolType())), new VariableEnvironment()));
Assert.Throws<Exception>(() => shisoku.CalcExpression.Calc(new MulExpression(new BoolExpression(true), new BoolExpression(true), new Checked(new BoolType())), new VariableEnvironment()));
}
[Fact]
public void DivExpressionDoesNotEvaluateWhenBooleanTypeArgmentsAreGiven()
{
Assert.Throws<Exception>(() => shisoku.CalcExpression.Calc(new DivExpression(new BoolExpression(true), new BoolExpression(true),new Checked(new BoolType())), new VariableEnvironment()));
Assert.Throws<Exception>(() => shisoku.CalcExpression.Calc(new DivExpression(new BoolExpression(true), new BoolExpression(true), new Checked(new BoolType())), new VariableEnvironment()));
}
[Fact]
public void functionExpressionCanEvaluate()
{
var expectedValue = new FunctionValue(new List<string>(), new Statement[] { }, new VariableEnvironment());
var result = shisoku.CalcExpression.Calc(new FunctionExpression(new List<(string,Type)>(), new Statement[] { }, new IntType()), new VariableEnvironment());
var result = shisoku.CalcExpression.Calc(new FunctionExpression(new List<(string, Type)>(), new Statement[] { }, new IntType()), new VariableEnvironment());
switch (result)
{
case FunctionValue(var arguments, var body, var env):
Expand All @@ -137,7 +137,7 @@ public void FunctionExpressionCanEvaluate()
var env = new VariableEnvironment();
env.Add("func", new FunctionValue(new List<string>(), new Statement[] { }, new VariableEnvironment()));
var expectedValue = new FunctionValue(new List<string>(), new Statement[] { }, env);
var result = shisoku.CalcExpression.Calc(new RecursiveFunctionExpression(new List<(string,Type)>(), new Statement[] { }, "fanc",new IntType()), new VariableEnvironment());
var result = shisoku.CalcExpression.Calc(new RecursiveFunctionExpression(new List<(string, Type)>(), new Statement[] { }, "fanc", new IntType()), new VariableEnvironment());
switch (result)
{
case FunctionValue(var arguments, var body, _):
Expand All @@ -155,7 +155,7 @@ public void FunctionCallCanEvaluate()
{
var expression = new CallExpression(
new (string, Expression)[] { },
new FunctionExpression(new List<(string,Type)>(),
new FunctionExpression(new List<(string, Type)>(),
new Statement[] {
new StatementReturn(new AddExpression(
new NumberExpression(1),new NumberExpression(2),new Checked(new IntType())
Expand All @@ -181,7 +181,7 @@ public void functionCanHaveConst()
new VariableEnvironment()
);
var result = shisoku.CalcExpression.Calc(new FunctionExpression(
new List<(string,Type)>(),
new List<(string, Type)>(),
new Statement[] {
new StatementConst("x", new IntType(), new NumberExpression(12)),
new StatementReturn(new VariableExpression("x", new Checked(new IntType())))
Expand Down Expand Up @@ -222,7 +222,7 @@ public void CallFunctionCanCalc()
new CallExpression(
new (string, Expression)[] { ("x", new NumberExpression(12)) },
new FunctionExpression(
new List<(string,Type)>() { ("x",new IntType()) },
new List<(string, Type)>() { ("x", new IntType()) },
new Statement[] {
new StatementReturn(new VariableExpression("x", new Checked(new IntType())))
},
Expand All @@ -243,7 +243,7 @@ public void CallFunctionCannotCalcWithExcessiveArguments()
new CallExpression(
new (string, Expression)[] { ("x", new NumberExpression(12)), ("y", new NumberExpression(13)) },
new FunctionExpression(
new List<(string,Type)>() { ("x", new IntType()) },
new List<(string, Type)>() { ("x", new IntType()) },
new Statement[] {
new StatementReturn(new VariableExpression("x", new Checked(new IntType())))
},
Expand All @@ -264,7 +264,7 @@ public void CallFunctionCannotCalcWithInsufficientArguments()
new CallExpression(
new (string, Expression)[] { ("x", new NumberExpression(12)) },
new FunctionExpression(
new List<(string,Type)>() { ("x", new IntType()), ("y", new IntType()) },
new List<(string, Type)>() { ("x", new IntType()), ("y", new IntType()) },
new Statement[] {
new StatementReturn(new AddExpression(
new VariableExpression("x", new Checked(new IntType())),
Expand Down
Loading

0 comments on commit 17b37f7

Please sign in to comment.