Add catches_strings test to test throws char * typemap

This commit is contained in:
William S Fulton 2022-09-18 19:37:02 +01:00
commit 4a29229bab
21 changed files with 286 additions and 0 deletions

View file

@ -0,0 +1,13 @@
%module catches_strings
%include <std_string.i>
%catches(const char *) StringsThrower::charstring;
%inline %{
struct StringsThrower {
static void charstring() {
throw "charstring message";
}
};
%}

View file

@ -133,6 +133,7 @@ CPP_TEST_CASES += \
bloody_hell \
bools \
catches \
catches_strings \
cast_operator \
casts \
char_binary \

View file

@ -0,0 +1,20 @@
using System;
using catches_stringsNamespace;
public class catches_strings_runme {
public static void Main()
{
{
bool exception_thrown = false;
try {
StringsThrower.charstring();
} catch (ApplicationException e) {
if (!e.Message.Contains("charstring message"))
throw new ApplicationException("incorrect exception message:" + e);
exception_thrown = true;
}
if (!exception_thrown)
throw new ApplicationException("Should have thrown an exception");
}
}
}

View file

@ -0,0 +1,20 @@
module catches_strings_runme;
import catches_strings.catches_strings;
import catches_strings.StringsThrower;
import std.algorithm;
void main() {
{
bool exception_thrown = false;
try {
StringsThrower.charstring();
} catch (Exception e) {
if (!canFind(e.msg, "charstring message"))
throw new Exception("incorrect exception message:" ~ e.msg);
exception_thrown = true;
}
if (!exception_thrown)
throw new Exception("Should have thrown an exception");
}
}

View file

@ -0,0 +1,20 @@
module catches_strings_runme;
import catches_strings.catches_strings;
import catches_strings.StringsThrower;
import std.algorithm;
void main() {
{
bool exception_thrown = false;
try {
StringsThrower.charstring();
} catch (Exception e) {
if (!canFind(e.msg, "charstring message"))
throw new Exception("incorrect exception message:" ~ e.msg);
exception_thrown = true;
}
if (!exception_thrown)
throw new Exception("Should have thrown an exception");
}
}

View file

@ -0,0 +1,17 @@
package main
import "strings"
import . "swigtests/catches_strings"
func main() {
exception_thrown := false
func() {
defer func() {
exception_thrown = strings.Index(recover().(string), "charstring message") == 0
}()
StringsThrowerCharstring()
}()
if !exception_thrown {
panic(0)
}
}

View file

@ -0,0 +1,3 @@
(dynamic-call "scm_init_catches_strings_module" (dynamic-link "./libcatches_strings"))
(load "testsuite.scm")
(load "../schemerunme/catches_strings.scm")

View file

@ -0,0 +1,28 @@
import catches_strings.*;
public class catches_strings_runme {
static {
try {
System.loadLibrary("catches_strings");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
System.exit(1);
}
}
public static void main(String argv[]) throws Throwable
{
{
boolean exception_thrown = false;
try {
StringsThrower.charstring();
} catch (RuntimeException e) {
if (!e.getMessage().contains("charstring message"))
throw new RuntimeException("incorrect exception message");
exception_thrown = true;
}
if (!exception_thrown)
throw new RuntimeException("Should have thrown an exception");
}
}
}

View file

@ -0,0 +1,15 @@
var catches_strings = require("catches_strings");
exception_thrown = false;
try {
catches_strings.StringsThrower.charstring();
} catch (e) {
console.log(typeof(e))
console.log(e.constructor.name)
console.log(typeof(e.message))
if (!e.message.includes("charstring message"))
throw new Error("incorrect exception message " + e.message);
exception_thrown = true;
}
if (!exception_thrown)
throw new Error("Should have thrown an exception");

View file

@ -0,0 +1,10 @@
require("import") -- the import fn
import("catches_strings") -- import code
-- catch "undefined" global variables
local env = _ENV -- Lua 5.2
if not env then env = getfenv () end -- Lua 5.1
setmetatable(env, {__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
s, msg = pcall(function() catches_strings.StringsThrower.charstring() end)
assert(s == false and msg:find("charstring message", 1, true))

View file

@ -0,0 +1,11 @@
(load-extension "catches_strings.so")
(require (lib "defmacro.ss"))
(define exception_thrown "no exception thrown for kin")
(with-handlers ([exn:fail? (lambda (exn)
(set! exception_thrown (exn-message exn)))])
(StringsThrower-charstring))
(unless (string-contains? exception_thrown "charstring message")
(error (format "incorrect exception message: ~a" exception_thrown)))
(exit 0)

View file

@ -0,0 +1,8 @@
open Swig
open Catches_strings
let _ =
try
ignore (_StringsThrower_charstring (C_void)); assert false
with Failure s ->
assert (s = "charstring message")

View file

@ -0,0 +1,19 @@
# do not dump Octave core
if exist("crash_dumps_octave_core", "builtin")
crash_dumps_octave_core(0);
endif
catches_strings
exception_thrown = false;
try
StringsThrower.charstring();
catch e
if (isempty(strfind(e.message, "charstring message")))
error("incorrect exception message: %s", e.message)
endif
exception_thrown = true;
end_try_catch
if (!exception_thrown)
error("Should have thrown an exception");
endif

View file

@ -0,0 +1,10 @@
use strict;
use warnings;
use Test::More tests => 3;
BEGIN { use_ok('catches_strings') }
require_ok('catches_strings');
eval {
catches_strings::StringsThrower::charstring();
};
like($@, qr/\bcharstring message/, "Should have thrown an exception");

View file

@ -0,0 +1,14 @@
<?php
require "tests.php";
$exception_thrown = false;
try {
StringsThrower::charstring();
} catch (Exception $e) {
check::str_contains($e->getMessage(), "charstring message", "incorrect exception message: {$e->getMessage()}");
$exception_thrown = true;
}
check::equal($exception_thrown, true, "Should have thrown an exception");
check::done();

View file

@ -0,0 +1,11 @@
from catches_strings import *
exception_thrown = False
try:
StringsThrower.charstring()
except RuntimeError as e:
if "charstring message" not in str(e):
raise RuntimeError("incorrect exception message:" + str(e))
exception_thrown = True
if not exception_thrown:
raise RuntimeError("Should have thrown an exception")

View file

@ -0,0 +1,15 @@
clargs <- commandArgs(trailing=TRUE)
source(file.path(clargs[1], "unittest.R"))
dyn.load(paste("catches_strings", .Platform$dynlib.ext, sep=""))
source("catches_strings.R")
cacheMetaData(1)
exception_thrown = FALSE
tryCatch({
StringsThrower_charstring()
}, error = function(e) {
exception_thrown <<- grepl(e$message, "charstring message", fixed=TRUE)
}
)
unittest(exception_thrown, TRUE)

View file

@ -0,0 +1,18 @@
#!/usr/bin/env ruby
require 'swig_assert'
require 'catches_strings'
exception_thrown = false
begin
Catches_strings::StringsThrower.charstring()
rescue RuntimeError => e
if (!e.to_s.include? "charstring message")
raise RuntimeError, "incorrect exception message: #{e.to_s}"
end
exception_thrown = true
end
if (!exception_thrown)
raise RuntimeError, "Should have thrown an exception"
end

View file

@ -0,0 +1,5 @@
(expect-throw 'swig-exception
(StringsThrower-charstring))
; TODO: check the exception message
(exit 0)

View file

@ -0,0 +1,10 @@
exec("swigtest.start", -1);
ierr = execstr("StringsThrower_charstring()", 'errcatch');
checkequal(ierr, 20000, "wrong/no exception thrown")
if (strstr(lasterror(), "charstring message") == '')
printf("Should have thrown an exception")
exit(1)
end
exec("swigtest.quit", -1);

View file

@ -0,0 +1,18 @@
if [ catch { load ./catches_strings[info sharedlibextension] catches_strings} err_msg ] {
puts stderr "Could not load shared object:\n$err_msg"
}
set exception_thrown 0
if [ catch {
StringsThrower_charstring
} e ] {
if {[string first "charstring message" $e] == -1} {
error "incorrect exception message: $e"
}
set exception_thrown 1
}
if {!$exception_thrown} {
error "Should have thrown an exception"
}