Skip to content

Commit

Permalink
Move reference stub out of main.rs
Browse files Browse the repository at this point in the history
* Put reference stub in stub.rs

---------

Co-authored-by: ellnix <[email protected]>
  • Loading branch information
Andriamanitra and ellnix authored Apr 21, 2024
1 parent 108af4d commit 30e2eb2
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 141 deletions.
35 changes: 1 addition & 34 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use clashlib::outputstyle::OutputStyle;
use clashlib::stub::StubConfig;
use clashlib::{solution, stub};
use directories::ProjectDirs;
use indoc::indoc;
use rand::seq::IteratorRandom;

#[derive(Clone)]
Expand Down Expand Up @@ -502,39 +501,7 @@ impl App {
input
}
Some(fname) => std::fs::read_to_string(fname)?,
None if args.get_flag("from-reference") => {
const REFERENCE_STUB: &str = indoc! {r##"
read anInt:int
read aFloat:float
read Long:long
read aWord:word(1)
read boolean:bool
read ABC1ABc1aBC1AbC1abc1:int
read STRING:string(256)
read anInt2:int aFloat2:float Long2:long aWord2:word(1) boolean2:bool
loop anInt read x:int
loop anInt read x:int f:float
loop anInt loop anInt read x:int y:int
loopline anInt x:int
loopline anInt w:word(50)
loopline anInt x:int f:float w:word(50)
write result
OUTPUT
An output comment
write join(anInt, aFloat, Long, boolean)
write join(aWord, "literal", STRING)
STATEMENT
This is the statement
INPUT
anInt: An input comment over anInt
"##};
REFERENCE_STUB.to_owned()
}
None if args.get_flag("from-reference") => stub::SIMPLE_REFERENCE_STUB.to_owned(),
None => {
let handle = self.current_handle()?;
self.read_clash(&handle)?
Expand Down
250 changes: 143 additions & 107 deletions src/stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod renderer;
pub mod stub_config;

use anyhow::Result;
use indoc::indoc;
pub use language::Language;
use serde::Serialize;
pub use stub_config::StubConfig;
Expand Down Expand Up @@ -131,134 +132,169 @@ pub enum Cmd {
},
}

pub const SIMPLE_REFERENCE_STUB: &str = indoc! {r##"
read anInt:int
read aFloat:float
read Long:long
read aWord:word(1)
read boolean:bool
read ABC1ABc1aBC1AbC1abc1:int
read STRING:string(256)
read anInt2:int aFloat2:float Long2:long aWord2:word(1) boolean2:bool
loop anInt read x:int
loop anInt read x:int f:float
loop anInt loop anInt read x:int y:int
loopline anInt x:int
loopline anInt w:word(50)
loopline anInt x:int f:float w:word(50)
write result
OUTPUT
An output comment
write join(anInt, aFloat, Long, boolean)
write join(aWord, "literal", STRING)
STATEMENT
This is the statement
INPUT
anInt: An input comment over anInt
"##};

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_simple_code_generation() {
let cfg = StubConfig::read_from_embedded("ruby").unwrap();
let generator = "read m:int n:int\nwrite result";
let received = generate(cfg, generator).unwrap();
let expected = "m, n = gets.split.map(&:to_i)\nputs \"result\"";
const COMPLEX_REFERENCE_STUB: &str = indoc! {r##"
write many spaces here
assert_eq!(received, expected);
}
read try:bool
read nil:string(50)
read L:string(20)
const REFERENCE_STUB: &str = r##"write many spaces here
OUTPUT
The spacemaster
read try:bool
read nil:string(50)
read L:string(20)
OUTPUT
The spacemaster
INPUT
a: does stuff
read a:word(50) b:word(50)
read xTra:int y:int
read annoying:word(xTra)
read anotherAnnoying:word(y)
read aBc:string(256)
read ROW:string(1024)
INPUT
a: does stuff
INPUT
ROW: Your boat
This is ignored
aBc: The alphabet
read a:word(50) b:word(50)
read xTra:int y:int
read annoying:word(xTra)
read anotherAnnoying:word(y)
read aBc:string(256)
read ROW:string(1024)
loop N read EXT:word(100) MT:word(100)
loop N read count:int name:word(50)
INPUT
ROW: Your boat
This is ignored
aBc: The alphabet
loop Q read FNAME:string(500)
loop N read EXT:word(100) MT:word(100)
loop N read count:int name:word(50)
loop 4 read number:int
loop Q read FNAME:string(500)
loop 4 write 0 0
loop 4 read number:int
read n:int
loop
n
loop 4
write thing
write thing
loop 4 write 0 0
write thing
read n:int
loop
n
loop 4
write thing
write thing
read xCount:int
loopline xCount x:int
loopline xCount x:int y:int z:word(50)
STATEMENT junk
Live long
and prosper
and a line with spaces both sides
write thing
write something something join(a, b)
write something join(a, b) something
write join(a, "b", aBc)
write join("hello", "world")
write join("hello", a, "planet")
"##};

read xCount:int
loopline xCount x:int
loopline xCount x:int y:int z:word(50)
STATEMENT junk
Live long
and prosper
and a line with spaces both sides
#[test]
fn test_simple_code_generation() {
let cfg = StubConfig::read_from_embedded("ruby").unwrap();
let generator = "read m:int n:int\nwrite result";
let received = generate(cfg, generator).unwrap();
let expected = "m, n = gets.split.map(&:to_i)\nputs \"result\"";

write something something join(a, b)
write something join(a, b) something
write join(a, "b", aBc)
write join("hello", "world")
write join("hello", a, "planet")"##;
assert_eq!(received, expected);
}

#[test]
fn test_reference_stub_ruby() {
let cfg = StubConfig::read_from_embedded("ruby").unwrap();
let received = generate(cfg, REFERENCE_STUB).unwrap();
let expected = r##"# Live long
# and prosper
# and a line with spaces both sides
# The spacemaster
puts "many spaces here"
try = gets.to_bool
_nil = gets.chomp
l = gets.chomp
a, b = gets.split
x_tra, y = gets.split.map(&:to_i)
annoying = gets
another_annoying = gets
a_bc = gets.chomp # The alphabet
row = gets.chomp # Your boat
n.times do
ext, mt = gets.split
end
n.times do
count, name = gets.split
count = count.to_i
name = name.chomp
end
q.times do
fname = gets.chomp
end
4.times do
number = gets.to_i
end
4.times do
puts "0 0"
end
n = gets.to_i
n.times do
4.times do
puts "thing"
puts "write thing"
end
end
puts "thing"
x_count = gets.to_i
gets.split.each do |x|
x = x.to_i
end
gets.split.each_slice(3) do |x, y, z|
x = x.to_i
y = y.to_i
end
puts "#{a} #{b}"
puts "#{a} #{b}"
puts "#{a} b #{a_bc}"
puts "hello world"
puts "hello #{a} planet""##;
let received = generate(cfg, COMPLEX_REFERENCE_STUB).unwrap();
let expected = indoc! { r##"
# Live long
# and prosper
# and a line with spaces both sides
# The spacemaster
puts "many spaces here"
try = gets.to_bool
_nil = gets.chomp
l = gets.chomp
a, b = gets.split
x_tra, y = gets.split.map(&:to_i)
annoying = gets
another_annoying = gets
a_bc = gets.chomp # The alphabet
row = gets.chomp # Your boat
n.times do
ext, mt = gets.split
end
n.times do
count, name = gets.split
count = count.to_i
name = name.chomp
end
q.times do
fname = gets.chomp
end
4.times do
number = gets.to_i
end
4.times do
puts "0 0"
end
n = gets.to_i
n.times do
4.times do
puts "thing"
puts "write thing"
end
end
puts "thing"
x_count = gets.to_i
gets.split.each do |x|
x = x.to_i
end
gets.split.each_slice(3) do |x, y, z|
x = x.to_i
y = y.to_i
end
puts "#{a} #{b}"
puts "#{a} #{b}"
puts "#{a} b #{a_bc}"
puts "hello world"
puts "hello #{a} planet"
"##};

for (r, e) in received.lines().zip(expected.lines()) {
assert_eq!(r, e)
Expand All @@ -269,18 +305,18 @@ puts "hello #{a} planet""##;
#[test]
fn test_reference_stub_rust() {
let cfg = StubConfig::read_from_embedded("rust").unwrap();
generate(cfg, REFERENCE_STUB).unwrap();
generate(cfg, COMPLEX_REFERENCE_STUB).unwrap();
}

#[test]
fn test_reference_stub_c() {
let cfg = StubConfig::read_from_embedded("c").unwrap();
generate(cfg, REFERENCE_STUB).unwrap();
generate(cfg, COMPLEX_REFERENCE_STUB).unwrap();
}

#[test]
fn test_reference_stub_cpp() {
let cfg = StubConfig::read_from_embedded("cpp").unwrap();
generate(cfg, REFERENCE_STUB).unwrap();
generate(cfg, COMPLEX_REFERENCE_STUB).unwrap();
}
}

0 comments on commit 30e2eb2

Please sign in to comment.