implement FWRITE, test FEVAL too

This commit is contained in:
Gwenhael Le Moine 2021-12-16 11:02:42 +01:00
parent 2c9203f079
commit f27f485c8c
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
2 changed files with 28 additions and 6 deletions

View file

@ -23,13 +23,12 @@ module Rpl
Rpl::Lang::Core.eval( stack, dictionary )
end
# ( content filename mode -- ) write content into filename using mode (w, a, …)
# ( content filename -- ) write content into filename
def fwrite( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[string], %i[string], :any] )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[string], :any] )
path = args[1][:value][1..-2]
puts "File.open( #{path}, mode: #{args[0][:value][1..-2]} ) { |file| file.write( #{args[2][:value]} ) }"
File.open( path ) { |file| file.write( args[0][:value] ) }
File.write( File.absolute_path( args[0][:value][1..-2] ),
args[1][:value] )
[stack, dictionary]
end

View file

@ -5,7 +5,7 @@ require 'test/unit'
require_relative '../language'
class TestLanguageTimeDate < Test::Unit::TestCase
class TestLanguageFileSystem < Test::Unit::TestCase
def test_fread
lang = Rpl::Language.new
lang.run '"spec/test.rpl" fread'
@ -24,4 +24,27 @@ trrr
{ value: ["'trrr'"], type: :list }],
lang.stack
end
def test_feval
lang = Rpl::Language.new
lang.run '"spec/test.rpl" feval vars'
assert_equal [{ value: 27, base: 10, type: :numeric },
{ value: ["'trrr'"], type: :list }],
lang.stack
end
def test_fwrite
lang = Rpl::Language.new
lang.run '"Ceci est un test de fwrite" "spec/test_fwrite.txt" fwrite'
assert_equal [],
lang.stack
assert_true File.exist?( 'spec/test_fwrite.txt' )
written_content = File.read( 'spec/test_fwrite.txt' )
assert_equal '"Ceci est un test de fwrite"',
written_content
FileUtils.rm 'spec/test_fwrite.txt'
end
end