I would like to ask how to create a module on c++ for v lang.
For example
с++
int some_method() {
return 6
}
v
import my_c_module
my_c_module.some_method()
I will be grateful for any information
And can I call c++ class method in v lang?
In V every file in the root of a folder is part of the same module. Simple programs don't need to specify module name, in which case it defaults to 'main'.
V is a very modular language. Creating reusable modules is encouraged and is quite easy to do. To create a new module, create a directory with your module's name containing .v files with code:
cd ~/code/modules
mkdir mymodule
vim mymodule/myfile.v
Inside myfile.v
// myfile.v
module mymodule
// To export a function we have to use `pub`
pub fn say_hi() {
println('hello from mymodule!')
}
Now you can call your module
import mymodule
fn main() {
mymodule.say_hi()
}
You can create also your custom module, and here is the official docs
Related
Please help me understand Jest mocks.
I've put some dummy functions in a file:
// actions.js
export function f1() {
return 1
}
export function calls_f1() {
f1()
}
And then in my test file I'm trying to understand how to check that a function calls another function:
import * as actions from './actions.js'
describe("MOCKS", () => {
actions.f1 = jest.fn();
actions.calls_f1();
expect(actions.f1).toBeCalled();
});
But the test is failing saying the mock function wasn't called. I've also tried swapping the 2nd and 3rd lines of the test, to no avail.
My jest config is all good, I've actually been doing a bunch of other testing (in this same file) that works.
What am I missing here?
Note: The actual implementation of this (that I'm simplifying greatly here) involves an actions file that includes a public export function fetchStations() that calls a private (or, rather, not exported) export function _downloadStations(). I'm trying to test that _downloadStations() is called.
I'm using import * as actions only for convenience, so I can write that line and then use whatever functions that file exports under actions.whatever() (instead of having to add functions to the import statement when I decide to use them). If import * as actions has some effect I'm not noticing (as implied by brian below) then I certainly don't have to use it and can use import {thisAction, thatAction} from './actions' of course.
This line:
import * as actions from './actions.js'
binds the module exports from actions.js to actions...
...so setting actions.f1 to a mock function replaces the module export for f1...
...but this doesn't affect calls_f1 since it calls f1 directly.
If calls_f1 is changed to call the module export for f1 then it will call the mock function.
There are two ways to make that happen.
One way is to move f1 into its own module.
The other way is to note that ES6 modules "support cyclic dependencies automatically" (a major design goal of ES6 modules) so a module can import its own exports:
actions.js
import * as actions from './actions'; // <= import the module...
export function f1() {
return 1
}
export function calls_f1() {
actions.f1() // <= ...and use it to call f1
}
actions.test.js
import * as actions from './actions.js'
describe('actions', () => {
it('calls_f1 should call f1', () => {
actions.f1 = jest.fn();
actions.calls_f1();
expect(actions.f1).toBeCalled(); // Success!
})
})
Update
OP updated the question to indicate that the function in question is not exported from the module.
In that case it is just an internal implementation detail of the module and cannot be spied on directly.
Testing it would involve testing for the effects that it causes, and not directly spying on it to see if it was called.
I'm working on legacy environment and I have Typescript files which contain modules or/and classes
class SomeClass {
...
}
module AndAModule {
export namespace NestedNamespace {
...
}
}
Notice the lack of "export" keyword in the top level modules/classes.
I would like to test functions in the "NestedNamespace" using Jest library. However when I import the Typescript file in a test file:
var service = require("/path/to/file/SomeService.ts")
I have no access neither to a class nor module defined in the SomeService.ts file. If add the "export" keyword before module and class I'm able to access them through the object returned from require, however this breaks whole environment and other files, as after this, the module and the class are not present in the outFile generated by typescript.
How can I import desired modules from files, without adding any export statement?
If you have a function which is not being exported, then some other function which is exported, is calling that un-exported function. Test the exported method, and you will indirectly be testing the un-exported one.
Consider a module which exports a subroutine that connects to the Internet and returns a result:
unit module A;
sub download is export {
"result from internet" # Not the actual implementation, obviously.
}
And another module which imports and calls that subroutine:
use A; # imports &download into this lexical scope
unit module B;
sub do-something is export {
download().uc ~ "!!" # Does something which involves calling &download
}
Now I'd like to write unit tests for module B.
But I don't want the tests to actually connect to the Internet; I'd like them to use a mock version of subroutine download that is controlled by my test script:
use Test;
plan 2;
use B;
my $mock-result;
my &mock-download = -> { $mock-result }
# ...Here goes magic code that installs &mock-download
# as &download in B's lexical scope...
$mock-result = "fake result";
is do-something(), "FAKE RESULT!!", "do-something works - 1";
$mock-result = "foobar";
is do-something(), "FOOBAR!!", "do-something works - 2";
The problem is the missing magic code to override sub download...
In Perl 5, I think this could be achieved pretty easily using glob assignment, or even nicer with the help of Sub::Override or Test::MockModule.
But in Perl 6, the lexical scope of module B closes when it has finished compiling, and can thus no longer be modified by the time the test script is run (correct me if I'm wrong). So this approach doesn't seem possible.
How would one solve this task in Perl 6, then?
I.e. how would one write unit tests for B::do-something, without letting it call the real A::download?
The simplest approach might be to use wrap which is described in https://docs.perl6.org/language/functions#Routines but a prerequisite for that is the use soft; pragma which prevents inlining. You'd need to use soft; in module A:
unit module A;
use soft;
sub download is export {
"result from internet";
}
Module B:
unit module B;
use A;
sub do-something is export {
download.uc ~ "!!";
}
And the test script:
use Test;
use A;
use B;
&download.wrap({
"mock result";
});
is do-something, "MOCK RESULT!!", "mock a 'use'd sub";
# ok 1 - mock a 'use'd sub
What's C++'s using some_namespace::object equivalent in golang?
According to the question here
I can get using namespace common with statement below:
import (
. "common"
)
But this would import the entire namespace. Right now I only want to use, say platform definition, something like using common::platform
Is there an equivalent for this in Go, so that I don't have to type common.platform all the time?
The following code comes close in terms of readability, but is less efficient, since the compiler cannot inline function calls anymore.
import (
"fmt"
"strings"
)
var (
Sprintf = fmt.Sprintf
HasPrefix = strings.HasPrefix
)
And, it has the side-effect of importing the names fmt and strings into the file scope, which is something that C++'s using does not do.
There is currently no such functionality in Go.
That's not to say it will never be added: there is open proposal to add "Alias declarations" to the language.
As others said, it is not possible in Go. In Go you import packages, not functions or types from packages.
Note that you can easily achieve what you want if you create a helper package.
Let's say you want "using" the fmt.Println() and fmt.Printf() functions only. Create a helper package:
package helper
import "fmt"
func Println(a ...interface{}) (n int, err error) {
return fmt.Println(a...)
}
func Printf(format string, a ...interface{}) (n int, err error) {
return fmt.Printf(format, a...)
}
And where you want the C++'s "using" functionality, import using a dot .:
import . "helper"
func Something() {
Println("Hi")
Printf("Using format string: %d", 3)
}
The result is that only the exported identifiers of the helper package will be in scope, nothing else from the fmt package. You can use this single helper package to make functions available from packages other than fmt of course, too. helper can import any other packages and have a "proxy" or delegator function publishing their functionality.
Personally I don't feel the need of this. I would just import fmt and call its functions using fmt.Println() and fmt.Printf().
Perhaps you could rename the package:
import (
c "common"
cout2 "github.com/one/cout"
cout2 "github.com/two/cout"
)
Then you would only have to type c.Platform
I have a bunch of modules defined in an Ember CLI app and each starts with the same path. I would like to import the modules into a module in the app. For example, I could write:
import post1 from 'posts/1';
import post2 from 'posts/2';
import post3 from 'posts/3';
export default Em.ObjectController.extend({
posts: Em.A(post1, post2, post3),
});
However, I do not know the module names because they are created/named on the fly by a precompiler. All I know is that the path always begins with the same string. In this case, posts.
Is there a way to import all modules that begin with a particular path? For example, how can I do something like the following:
import posts from 'posts/*';
// or
registry['posts'].forEach(postId, i) {
var path = 'posts/' + postId;
import i from path;
}
Each of the modules I want to find and import has exported an object.
I have been through the ES6 module transpiler docs but can't find much.
The ES6 spec doesn't allow you to dynamically import modules using the import keyword. All importing and exporting using the module syntax must be done statically. However, it does provide a programmatic API that you can use to dynamically import modules. This article has a great summary of it (as well as a great summary of the rest of ES6 modules).
My suggestion would be to wrap Ember's loader API in a ES6 compliant wrapper. For instance, Ember-CLI uses the require() function to get modules, so you could do something like this:
window.System = window.System || {};
window.System['import'] = function(moduleName) {
return Ember.RSVP.Promise.resolve(window.require(moduleName));
}
// ...
System.import('my-app/posts/' + postNumber).then(function(postModule) {
// ...
});
You could also use the Ember require() function directly, my way just protects you in the likely event that Ember changes their module loader.