How can I fake a file?

I'd like to write a sketch which does depend on a file from the SD card in an Ethernet shield v2. As I cannot use the shield at the moment I'd like to write and run the code on the Arduino only. This requires that I can use a fake file instead of the actual one read from the SD card. The machanism I need to use is mock/fake object.

How can I fake a file comparable to I'd do in a non-embedded environment?

I'm not sure what the problem is, or why you would need a "fake file". What is the contents of that file supposed to be? Maybe some connection parameter? Couldn't you hard-code that data for testing purposes?

Actually the content of the file does not matter. It's about the programming methodology to get rid of dependencies (in this case of the hardware). If you do not understand the question you'll be not able to answer it :slight_smile:

If you don't state your problem clearly, you won't be able to get useful answers. Good luck.

@330R I've added a hint to wikipedia above. Probably this helps...

So, given that hint, let's see the code you've written.

I've not written any code yet cause I don't know how to fake objects on Arduino :slight_smile: That's what the question is about...

"Arduino" is essentially C++. So, write your fake objects as you normally would in that language.

Ah, right... However File is specific to the Arduino SD lib which depends on the actual hardware.

You could use conditional compilation for that:

#define MOCKUP_FILE

#ifdef MOCKUP_FILE
  #include "mockupfile.h"
  MockupFile myFile();
#else
  File myFile;
#endif

File myFile;

void open_file(char* name)
{
#ifndef MOCKUP_FILE
  myFile = SD.open(name);
#endif
}

Create the mockupfile.h header which basically contains an empty class derived from the File object.

You could write a function that would get a line from a file but have it read from serial instead. Alternatively, it could return lines from a hard coded array.

That's what I'm aiming for.

I'd prefer a solutions which does not depend on the filesystem at all but uses "in-memory" data only. However rthanks for the hint.

Of course it is. You haven't specified which "Arduino" board you're using. But, assuming an Uno, see SD.h:

  class File : public Stream {
    private:
      char _name[13]; // our name
      SdFile *_file;  // underlying file pointer

    public:
      File(SdFile f, const char *name);     // wraps an underlying SdFile
      File(void);      // 'empty' constructor
      virtual size_t write(uint8_t);
      virtual size_t write(const uint8_t *buf, size_t size);
      virtual int availableForWrite();
      virtual int read();
      virtual int peek();
      virtual int available();
      virtual void flush();
      int read(void *buf, uint16_t nbyte);
      boolean seek(uint32_t pos);
      uint32_t position();
      uint32_t size();
      void close();
      operator bool();
      char * name();

      boolean isDirectory(void);
      File openNextFile(uint8_t mode = O_RDONLY);
      void rewindDirectory(void);

      using Print::write;
  };

Yes, I found out and corrected my comment on this

So I could patch the SD library to include mock capabilities. Thanks, that's everything I need to know right now.

I wouldn't modify the existing library. Make your own version that implements the interface of the File class. Then do one of the of the following depending on whether the SD hardware is connected or not:

#include "MySD.h"
#include <SD.h>

Right, that's even better. However I won't need it ATM.

good decision.
Imho mocking a SD card makes not so much sense on an UNO.
Consider your mock might need to handle file states, position in file for peek and see, ... just see the list of gfvalvo - most of them would need a simulated result. May be also file content.
You will end up to enrich your empty mock with as much functionality, needing resources you might not have available on an Uno with 2KB SRAM.
Just use a real SD Card, and invest the time in your sketch - not in that mock.

@noiasca That's exaclty what I'm dooing right now. Probably I'll introduce some functions which separate IO from logic concerns. The functions expect to get a stream or something passed, the IO functions are just responsible for opening + reading/writing.