diff options
author | William S Fulton <wsf@fultondesigns.co.uk> | 2022-09-18 19:37:02 +0100 |
---|---|---|
committer | William S Fulton <wsf@fultondesigns.co.uk> | 2022-09-19 09:09:29 +0100 |
commit | 4a29229bab06cd5ab21715c93c03e9280feae065 (patch) | |
tree | 756bffae629b36a5cf505af7b36eb32d4f99c5a0 | |
parent | 034e2358f9302983cd974746a548d10b45b46cf1 (diff) | |
download | swig-4a29229bab06cd5ab21715c93c03e9280feae065.tar.gz |
Add catches_strings test to test throws char * typemap
21 files changed, 286 insertions, 0 deletions
diff --git a/Examples/test-suite/catches_strings.i b/Examples/test-suite/catches_strings.i new file mode 100644 index 000000000..843246e23 --- /dev/null +++ b/Examples/test-suite/catches_strings.i @@ -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"; + } +}; +%} diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk index 4df120943..64057fbcb 100644 --- a/Examples/test-suite/common.mk +++ b/Examples/test-suite/common.mk @@ -133,6 +133,7 @@ CPP_TEST_CASES += \ bloody_hell \ bools \ catches \ + catches_strings \ cast_operator \ casts \ char_binary \ diff --git a/Examples/test-suite/csharp/catches_strings_runme.cs b/Examples/test-suite/csharp/catches_strings_runme.cs new file mode 100644 index 000000000..6eb159141 --- /dev/null +++ b/Examples/test-suite/csharp/catches_strings_runme.cs @@ -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"); + } + } +} diff --git a/Examples/test-suite/d/catches_strings_runme.1.d b/Examples/test-suite/d/catches_strings_runme.1.d new file mode 100644 index 000000000..d42035100 --- /dev/null +++ b/Examples/test-suite/d/catches_strings_runme.1.d @@ -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"); + } +} diff --git a/Examples/test-suite/d/catches_strings_runme.2.d b/Examples/test-suite/d/catches_strings_runme.2.d new file mode 100644 index 000000000..d42035100 --- /dev/null +++ b/Examples/test-suite/d/catches_strings_runme.2.d @@ -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"); + } +} diff --git a/Examples/test-suite/go/catches_strings_runme.go b/Examples/test-suite/go/catches_strings_runme.go new file mode 100644 index 000000000..6f0be2c9b --- /dev/null +++ b/Examples/test-suite/go/catches_strings_runme.go @@ -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) + } +} diff --git a/Examples/test-suite/guile/catches_strings_runme.scm b/Examples/test-suite/guile/catches_strings_runme.scm new file mode 100644 index 000000000..376f235a3 --- /dev/null +++ b/Examples/test-suite/guile/catches_strings_runme.scm @@ -0,0 +1,3 @@ +(dynamic-call "scm_init_catches_strings_module" (dynamic-link "./libcatches_strings")) +(load "testsuite.scm") +(load "../schemerunme/catches_strings.scm") diff --git a/Examples/test-suite/java/catches_strings_runme.java b/Examples/test-suite/java/catches_strings_runme.java new file mode 100644 index 000000000..0029327d4 --- /dev/null +++ b/Examples/test-suite/java/catches_strings_runme.java @@ -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"); + } + } +} diff --git a/Examples/test-suite/javascript/catches_strings_runme.js b/Examples/test-suite/javascript/catches_strings_runme.js new file mode 100644 index 000000000..be37c8710 --- /dev/null +++ b/Examples/test-suite/javascript/catches_strings_runme.js @@ -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"); diff --git a/Examples/test-suite/lua/catches_strings_runme.lua b/Examples/test-suite/lua/catches_strings_runme.lua new file mode 100644 index 000000000..b23609f11 --- /dev/null +++ b/Examples/test-suite/lua/catches_strings_runme.lua @@ -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)) diff --git a/Examples/test-suite/mzscheme/catches_strings_runme.scm b/Examples/test-suite/mzscheme/catches_strings_runme.scm new file mode 100644 index 000000000..43d313de0 --- /dev/null +++ b/Examples/test-suite/mzscheme/catches_strings_runme.scm @@ -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) diff --git a/Examples/test-suite/ocaml/catches_strings_runme.ml b/Examples/test-suite/ocaml/catches_strings_runme.ml new file mode 100644 index 000000000..610f23bd9 --- /dev/null +++ b/Examples/test-suite/ocaml/catches_strings_runme.ml @@ -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") diff --git a/Examples/test-suite/octave/catches_strings_runme.m b/Examples/test-suite/octave/catches_strings_runme.m new file mode 100644 index 000000000..75ff6c10b --- /dev/null +++ b/Examples/test-suite/octave/catches_strings_runme.m @@ -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 diff --git a/Examples/test-suite/perl5/catches_strings_runme.pl b/Examples/test-suite/perl5/catches_strings_runme.pl new file mode 100644 index 000000000..a35095f02 --- /dev/null +++ b/Examples/test-suite/perl5/catches_strings_runme.pl @@ -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"); diff --git a/Examples/test-suite/php/catches_strings_runme.php b/Examples/test-suite/php/catches_strings_runme.php new file mode 100644 index 000000000..ef945f0b5 --- /dev/null +++ b/Examples/test-suite/php/catches_strings_runme.php @@ -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(); diff --git a/Examples/test-suite/python/catches_strings_runme.py b/Examples/test-suite/python/catches_strings_runme.py new file mode 100644 index 000000000..16ad6b056 --- /dev/null +++ b/Examples/test-suite/python/catches_strings_runme.py @@ -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") diff --git a/Examples/test-suite/r/catches_strings_runme.R b/Examples/test-suite/r/catches_strings_runme.R new file mode 100644 index 000000000..2eb4f3c83 --- /dev/null +++ b/Examples/test-suite/r/catches_strings_runme.R @@ -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) diff --git a/Examples/test-suite/ruby/catches_strings_runme.rb b/Examples/test-suite/ruby/catches_strings_runme.rb new file mode 100644 index 000000000..5af873ef7 --- /dev/null +++ b/Examples/test-suite/ruby/catches_strings_runme.rb @@ -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 diff --git a/Examples/test-suite/schemerunme/catches_strings.scm b/Examples/test-suite/schemerunme/catches_strings.scm new file mode 100644 index 000000000..5be2d2345 --- /dev/null +++ b/Examples/test-suite/schemerunme/catches_strings.scm @@ -0,0 +1,5 @@ +(expect-throw 'swig-exception + (StringsThrower-charstring)) +; TODO: check the exception message + +(exit 0) diff --git a/Examples/test-suite/scilab/catches_strings_runme.sci b/Examples/test-suite/scilab/catches_strings_runme.sci new file mode 100644 index 000000000..4c6718a49 --- /dev/null +++ b/Examples/test-suite/scilab/catches_strings_runme.sci @@ -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); diff --git a/Examples/test-suite/tcl/catches_strings_runme.tcl b/Examples/test-suite/tcl/catches_strings_runme.tcl new file mode 100644 index 000000000..1ada351e6 --- /dev/null +++ b/Examples/test-suite/tcl/catches_strings_runme.tcl @@ -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" +} |